In this lesson, we will explain the use of structs in C#, when to use them, and the different between structs and classes.

C# Struct

Structure or struct is a value type that can contains variables, functions, methods, constructors, indexers, operators, event, and properties. Structs can implement interface, however they cannot inherit from other struct or class.

Syntax of struct

    struct Struct_Name
    {
        //Structure members   
    }

Example of struct in C#

namespace TutorialsPanel
{
    class Program
    {
        struct Car
        {

            //Variables   

            String make;

            String power;

            String color;

            int _horsepower;

            public static void Start()
            {
                Console.WriteLine("Engine Starts");
            }

            public void Horsepower()
            {
                Console.WriteLine("Car hourse power is: " + _horsepower);
            }
            //Property   

            public int Color { get; set; }
        }

        static void Main(string[] args)
        {

            Car.Start(); // Call a static member

            Console.ReadKey();

        }

    }

}

Structs and interfaces

In C#, a struct can implement any number of interfaces. Example below demonstrates how the struct Car can implement 2 interfaces, Vehicle and Dealer.

namespace TutorialsPanel
{
    class Program
    {
        interface iVehicule
        {

        }

        interface iDealer
        {
            void GetAllDealers(int DealersNumber);
        }

        struct Car : iDealer, iVehicule
        {

            //Variables   

            string make;
            string power;
            string color;
            int _horsepower;

            //Method from iDealers interface is defined   

            public void GetAllDealers(int numberOfDealers)
            {
                Console.WriteLine("There are " + numberOfDealers + " dealers in the area");
            }
        }

        static void Main(string[] args)
        {
            //struct object is created   

            Car _car = new Car();
            _car.GetAllDealers(12);
            Console.ReadKey();
        }
    }
}

Output

There are 12 dealers in the area

Difference between Class and Structure

  • Struct is light weight and performs better than a class.
  • Strut is used for small collection that does not requires complication, such as circle, square, or Calendar structs.
  • Structs are great when all its members are value type, while class performs better with both value and reference.
  • A struct is a value type, while the class a reference type. Using a struct over class will help the performance of the application, as it results in lee load of garbage collector, fewer objects in the heap. However, larger struts tend to be more expensive. If you are dealing with a large object, create a class instead.

Key to remember:

  • We cannot define a default constructor in a struct. However, we can define static and parametrized constructors.
  • A struct cannot contain destructors.
  • A Struct is a value type.
  • Use structs for small objects.
  • A struct can implement multiple interfaces. However, it cannot inherit from another strut or class. Also, a struct cannot be used as a base class.
Last modified: January 30, 2019

Comments

Write a Reply or Comment

Your email address will not be published.