Custom Search

Wednesday, March 5, 2008

Structures (C#)

A Structure in C# is a composite data type containing many number of different data types. A Struct in C# can contain methods, constructors, fields, other structure type, properties, constants and indexers.

To declare a structure you use the struct keyword:

<modifiers> struct <struct_name>

For example:

public class Program

public struct St { //Could be private, internal or protected

public int val;
public String text;


public static void Main(string[] args)


//Initializing the struct

St s = new St();

//Adding values to the struct members

s.text = "Using struct";
s.val = 2;

Console.WriteLine("Value: " + s.val + " - Text: " + s.text);


No comments: