Pages

Sunday, 28 June 2015

Structs

Structs

Structs are programmer-defined data types, very similar to classes. They have data members and function members. Although structs are similar to classes, there are a number of important differences. The most important ones are the following:
  • Classes are reference types, and structs are value types.
  • Unlike classes, struct's not allowing field initializers
  • Structs are implicitly sealed, which means they cannot be derived from.
Syntax for declaring a struct is similar to that of declaring a class:
struct [Struct Name]
{
 [Member Declarations]
}
For example, the following code declares a struct named Point.
struct Point
{
 public int X;
 public int Y;
}
class Program
{
 static void Main()
 {
  Point first, second, third;
  first.X = 10; first.Y = 10;
  second.X = 20; second.Y = 20;
  third.X = first.X + second.X;
  third.Y = first.Y + second.Y;
  Console.WriteLine("first Point: " + first.X + "," + first.Y);
  Console.WriteLine("second Point: " + second.X + "," + second.Y);
  Console.WriteLine("third Point: " + third.X + "," + third.Y);
 }
}

This code produces the following output:
first: 10, 10
second: 20, 20
third: 30, 30

Constructors and Destructors

Structs can have instance and static constructors, but destructors are not allowed.

The language implicitly supplies a parameterless constructor for every struct. This constructor sets each of the struct’s members to the default value for that type. Value members are set to their default values.

The predefined parameterless constructor exists for every struct—and you cannot delete or redefine it. You can, however, create additional constructors, as long as they have parameters. Notice that this is different from classes. For classes, the compiler supplies an implicit parameterless constructor only if no other constructors are declared.

For example:
   
struct Point
{
 public int X;
 public int Y;
 public Point(int a, int b) // Constructor with parameters
 {
  X = a;
  Y = b;
 }
}
class Program
{
 static void Main()
 {
  Point s1 = new Point();
  Point s2 = new Point(5, 10);

  Console.WriteLine(s1.X +","+s1.Y);
  Console.WriteLine(s2.X + "," + s2.Y);
 }
}

You can also create an instance of a struct without using the new operator. If you do this, however, there are several restrictions, which are the following:
  • Can not use the value of a data member until you have explicitly set it.
  • Can not call any function member of the struct until all the data members have been assigned.
See Following example:
   
struct Point
{
 public int X;
 public int Y;
 public Point(int a, int b) // Constructor with parameters
 {
  X = a;
  Y = b;
 }
}
class Program
{
 static void Main()
 {
  Point s1 , s2;
  Console.WriteLine("{0},{1}", s1.X, s1.Y) ;   // Compiler error
  
   s2.X = 5;           //Not yet assigned
   s2.Y = 10;
   Console.WriteLine("{0},{1}", s2.X, s2.Y); // OK
 }
}

Static Constructors

As with classes, the static constructors of structs create and initialize the static data members and cannot reference instance members. Static constructors for structs follow the same rules as those for classes.
A static constructor is called before the first of either of the following two actions:
  • A call to an explicitly declared constructor
  • A reference to a static member of the struct

Summary of Constructors and Destructors

Type Description
Instance constructor (parameterless) Cannot be declared in the program. An implicit constructor is always supplied by the system for all structs. It cannot be deleted or redefined by the program.
Instance constructor (with parameters) Can be declared in the program.
Static constructor Can be declared in the program.
Destructor Cannot be declared in the program. Destructors are not allowed.

Structs As Return Values and Parameters

Structs can be used as return values and parameters.
  • Return value: When a struct is a return value, a copy is created and returned from the function member.
  • Value parameter: When a struct is used as a value parameter, a copy of the actual parameter struct is created. The copy is used in the execution of the method.
  • ref and out parameters: If you use a struct as a ref or out parameter, a reference to the struct is passed into the method so that the data members can be changed.

0 comments:

Post a Comment