Friends, want to be lucid about what structures are in C# and other various issues concerning them? This article will help you! Let's get started!
Structures in C# are similar to structures in C and C++ but with several prominent differences. In C#, structures are value types. The memory for structure variables is allocated on stack and there is no heap allocation. Structures can have constructors, methods and properties!
The following are the possible structure modifiers:
- new
- public
- protected
- internal and
- private
Same modifiers cannot appear more than once in a declaration. The modifiers namely abstract and sealed are not permitted.
The notable differences between structures in C, C++ and C# are as follows:
- In C++, structure members are public by default while class members are private by default but in C# it is not the case.
- In C, we cannot have constructors in a structure.
- In C++ it is possible to have constructors for a structure.
- In C++ partial initialization of structures is allowed whereas in C#, it is not.
If you run through the following program, it will be much easier to follow
//structures in C#
//structure_test
//This program contains a structure 'struct1', a class 'structinside' and the Main class 'class1'
using System;
namespace structure_test
{
//structure
public struct struct1
{
//public struct1(){} structures cannot contain explicit parameterless constructors
public struct1(int ival, bool hasdataval, string strval)
{
this.i=ival;
this.hasdata=hasdataval;
this.str=strval;
}
public void show()
{
Console.WriteLine("i {0},hasdata {1},str {2}",i,hasdata,str);
}
//public int i=10; cannot have instant field initializers in structs
public int i;//structure members are private by default
public bool hasdata;
public string str;
}
//class
class structinside
{
public int a=5;
public struct1 structvar1;
// a class can have a structure variable as its member
}
//Main class
class Class1
{
[STAThread]
static void Main(string[] args)
{
//instantiating structinside class
structinside s = new structinside();
//instantiating the structure struct1
struct1 structvar2 = new struct1(1,true,"Saradha");
//displaying the structure instance structvar2
structvar2.show();
//initializing the members of structure variable structvar1 which is a member of the class structinside
s.structvar1.i=3;
s.structvar1.hasdata=false;
s.structvar1.str="Gnanavel";
s.structvar1.show();
//structure variables must be initialized before use
struct1 structvar_without_new;
//structvar_without_new.show();error: use of unassigned local variable
}
}
}
As you can see, in C#, structure members are private by default. Thus the structure fields should be declared public to get the default structure behavior.
We can't have instance field initializes for structure members. Structure members must be initialized using functions or through constructors.
We also cannot have explicit parameter less constructors (default constructors), since they are reserved. But we can have any number of custom parameterized constructors with different signatures.
While declaring a structure variable, if we do not use new operator to call a constructor, then the structure object will be created but the values will be unassigned. Remember in C#, all value types must be initialized before use. Also note that instance variables and class variables need not necessarily be initialized before use.