Pages

Men

rh

6/16/2012

Structures in C#

Structures:
Structs are similar to classes in that they represent data structures that can contain data members and function members. A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types.

Example 1

struct Student : IGrade
{   
    public int maths;
    public int english;
    public int csharp;
 
    //public member function
    public int GetTot()
    {
        return maths+english+csharp;
    }
 
    //We have a constructor that takes an int as argument
    public Student(int y)
    {
        maths = english = csharp = y;
    }
 
    //This method is implemented because we derive
    //from the IGrade interface
    public string GetGrade()
    {
        if(GetTot() > 240 )
            return "Brilliant";
        if(GetTot() > 140 )
            return "Passed";
        return "Failed";
    }
}
 
interface IGrade
{
    string GetGrade();
}
Well, now let's take a look at how we can use our struct.
          Student s1 = new Student();
Console.WriteLine(s1.GetTot());
Console.WriteLine(s1.GetGrade());
 
//Output
0
Failed


Here the default constructor gets called. This is automatically implemented for us and we cannot have our own default parameter-less constructor. The default parameter-less constructor simply initializes all values to their zero-equivalents. This is why we get a 0 as the total.
 
Student s2;
s2.maths = s2.english = s2.csharp = 50;
Console.WriteLine(s2.GetTot());
Console.WriteLine(s2.GetGrade());
 
//Output
150
Passed

When to use structs :

Because structs are value types they would be easier to handle and more efficient that classes. When you find that you are using a class mostly for storing a set of values, you must replace those classes with structs. When you declare arrays of structs because they are created on the heap, efficiency again improves. Because if they were classes each class object would need to have memory allocated on the heap and their references would be stored. In fact lots of classes within the .NET framework are actually structs.

 For example System.Drawing.Point is actually a struct and not a class.

Remarks

  • It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values.
  • It is an error to initialize an instance field in a struct.
  • When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs 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 of the fields are initialized.
  • There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.
  • Unlike C++, you cannot declare a class using the keyword struct. In C#, classes and structs are semantically different. A struct is a value type, while a class is a reference type.
  • Unless you need reference type semantics, a class that is smaller than 16 bytes may be more efficiently handled by the system as a struct.

 

 

 

No comments :

Post a Comment