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
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor