SIGN UP MEMBER LOGIN:    
ARTICLE

Digging Deeper - Structures in C#

Posted by Saradha Gnanavel Articles | C# Language February 22, 2005
Structures in C# are similar to structures in C and C++ but with several prominent differences. In C#, structures are value types.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

Very good article. i need one clarification what is the use of structures when we can do the job using classes only.

Posted by syed Jun 19, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor