Structure Initialization in C#

No.of Views819
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  10 Feb 2011 03:02:14
Tag : .NET Frameworks , How to
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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#

int 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.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    In this article i will explain about extension method and how to create in .NET
    Published Date : 28/Jan/2011
    The scope of this document is to describe integration of SAP Web Services and Microsoft .NET using Microsoft Visual Studio.
    Published Date : 18/Aug/2010
    Let us start this article by a small chat between customer and developer. Scenario 1 Customer: - How’s your application performance? Subjective developer: - Well it’s speedy, it’s the best …huuh aaa ooh it’s a like rocket. Scenario 2 Customer: - How’s your application performance? Quantitative developer: - With 2 GB RAM , xyz processor and 20000 customer records the customer screen load in 20 secs. I am sure the second developer looks more promising than the first developer. In this
    Published Date : 10/May/2010
    Ask any developer which is the best place in a .NET class to clean unmanaged resources?, 70% of them will say the destructor. Although it looks the most promising place for cleanup it has a huge impact on performance and memory consumption. Writing clean up code in the destructor leads to double GC visits and thus affecting the performance multifold times. In order to validate the same we will first start with bit of theory and then we will actually see how GC algorithm performance is impacte
    Published Date : 06/May/2010
    One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET.
    Published Date : 05/May/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top