Pranay Rana: Structure Initialization in C#

Saturday, February 12, 2011

Structure Initialization in C#

Structure in C# allows us to group the variables and methods. Its some what similar to classes but that's not true there are no. of difference between class and structure. But here in this post I am not going to discuss about that, here I am going to explain how to Initialize Structure.

Facts:
   1. Structure is Value type.
   2. D# oesn't allows to create parameter less constructor because its initialize the variable with the default values. 

Now consider the below case where I have created two structure
structure 1 : with the public members.
public struct StructMember
{
    public int  a;
    public int  b;
}
structure 2 : with the properties.
public struct StructProperties
{
       private int a;
       private int b;

       public int A
       {
               get
               { return a; }
               set
               { a = value; }
       }

       public int B
       {
               get
               { return b; }
               set
               { b = value; }
       }
}   
Now by considering above two facts in mind and I tried to use the both the structure as below
public class MainClass
{
    public static void Main()
    {
       StructMembers MembersStruct;

       StructProperties PropertiesStruct;

  
       MembersStruct.X = 100;
       MembersStruct.Y = 200;

       PropertiesStruct.X = 100;    
       PropertiesStruct.Y = 200;
    }
}
After doing this when I tried to compile the code I received below error

The C# compiler issues the following error:
error CS0165: Use of unassigned local variable  'PropertiesStruct'
So by this C# compiler informs that it allow to use the first structure without an error but not allow to use second structure which exposed property.

To resolve issue I wrote below line of the code to initialize second structure.
StructProperties PropertiesStruct = new  StructProperties();
And after doing this when I compiled code it gets complied successfully.

To find out the reason why it worked without error when I wrote second line, I reviewed code under dissembler ILDSAM and found that property of the second structure is get implemented as public function in MSIL.
Dissembled code
Image Loading
Fact is struct can be instantiated without using the New operator. If you do not use New, the fields will remain unassigned and the object cannot be used until all the fields are initialized. So this is why we need to use New operator to initialize.

In .Net all simple types are Structures so when you write code in C#
Consider case below where I am creating one integer variable
int a;
Console.WriteLine(a);
than compiler raise an error that you cannot use variable without initializing it.

so you need to write either
default constructor assigned the value 0 to myInt.
int a =0;
or
Using the new operator calls the default constructor of the specific type and assigns the default value to the variable.
int a = new int();

Summary
Its very important to initialize structure proper way when you are coding by making use of structure.

2 comments:

  1. When in doubt you can also use the default keyword.

    ReplyDelete
  2. Easily, the post is actually the greatest on this deserving topic. I agree with your conclusions and will thirstily look forward to your coming updates. . . . .St Louis Cardinals Hats,Tampa Bay Rays Hats,Canada Goose Coats

    ReplyDelete