Geeks With Blogs


Google My Blog

Murray Gordon Flash a-ah! Savior of the universe...



In C++ a struct is just about the same as a class for all purposes except in the default access modifier for methods. In C# a struct are a pale puny version of a class. I am not sure why this was done so, but perhaps they decided to have a clear distinction between structs and classes. Here are some of the drastic areas where classes and structs differ in functionality.

  • structs are stack objects and however much you try you cannot create them on the heap
  • structs cannot inherit from other structs though they can derive from interfaces
  • You cannot declare a default constructor for a struct, your constructors must have parameters
  • The constructor is called only if you create your struct using new, if you simply declare the struct just as in  declaring a native type like int, you must explicitly set each member's value before you can use the struct

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();








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;








Because we haven't used new, the constructor does not get called. Of all the silly features this one must win the annual contest by a long way. I see no sane reason why this must be so. Anyway you have to initialize all the member fields. If you comment out the line that does the initialization you will get a compiler error :- Use of unassigned local variable 's2'

Student s3 = new Student(90);








This time we use our custom constructor that takes an int as argument.


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.





Posted on Friday, September 30, 2005 10:33 AM | Back to top

Comments on this post: Structs in C# (pale puny version of a class)

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Murray Gordon | Powered by: