HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Structs in C#
Structs and classes are similar as both structs and classes are data structures that can contain data members and function members. Structs have have relatively lower over head when compared with classes and can be used to represent small data structures. Simple types in C# such as int, bool, double etc.. are examples of structs.All struct types implicitly inherit from the class System.ValueType. One common interview questions: What are the differences between a struct and a class. The following are the differences between a struct and a class.
  • Struct is a value type where as class is a reference type. For a clear understanding of Value types, reference types and differences please read Types in C# article.
  • Structs are stored on the stack where as classes are stored on the heap.
  • A struct cannot have a parameter less constructor. For a clear understanding of classes and different types of constructors please read classes in C# article.
  • A struct cannot have a destructor
  • Structs does not support inheritance where as class does. But remember strcuts can inherit interfaces.
A simple example of struct is shown below:
  • Structs cannot have parameter less constructors as structs are value types. A parameter less constructor for a struct will produce a compile time error.
  • We cannot have destructors for a struct. Destructors for a struct will produce compile time errors and this makes sense because, structs are value types and are destroyed immediately after the scope of the struct is lost and we don't need destructors to garbage collect the struct.
  • We can also have properties in structs. For a clear understanding of what are properties, get and set accessors and differences please read properties in C# article..
  • using System;

    public struct StructDemo

    {

        public int X;

        public int Y;

        //The parameter less constructor for a struct will

        //produce a compile time error.

        //public StructDemo()

        //{

        //}

       

        //Strcuts can have constructors with parameters

        public StructDemo(int A, int B)

        {

            X = A;

            Y = B;

        }

       

        //Destructors for a struct will produce compile time errors

        //~StructDemo()

        //{

        //}

    }

       

    public class MainClass

    {

        public static void Main()

        {

            StructDemo SD = new StructDemo(10, 20);

            Console.WriteLine("X = " + SD.X + " Y = " + SD.Y);

        }

    }

    A real time example is shown below.
    • All of us read about vectors. If I had to add 2 vectors which has an X and Y co - ordinates we can implement the same as shown in the example below using vectors.
    • We have a constructor which will initialize the X and Y co - ordinates with the passed in values.
    • AddVectors method takes Vector as a parameter. Inside this method we create ResultVector which can hold the resulting Vector X and Y co - ordinates. The ResultVector is then returned. For a clear understanding on whar are methods, static and instance methods and differences please read methods in C# article..
    • In the Main method we create V1 and V2 usng the constructor of the struct. We create V3 which holds the result after adding V1 and V2.

    using System;

    public struct Vector

    {

        public int X;

        public int Y;

     

        //Constructor

        public Vector(int A, int B)

        {

            X = A;

            Y = B;

        }

     

        public Vector AddVectors(Vector Vector2Add)

        {

            Vector ResultVector;

     

            ResultVector.X = this.X + Vector2Add.X;

            ResultVector.Y = this.Y + Vector2Add.Y;

            return ResultVector;

        }

     

    }

     

    public class MainClass

    {

        public static void Main()

        {

            Vector V1 = new Vector(10, 20);

            Vector V2 = new Vector(20, 10);

     

            Vector V3 = V1.AddVectors(V2);

     

            Console.WriteLine("V3.X = " + V3.X + " V3.Y = " + V3.Y);

        }

    }


    Njoy Programming
       ByPrasad Cherukuri