Defining Structs in C#

This article focuses on an alternative to classes in C#, structs. I'll also talk about their application and how they are an alternative to classes and where we can actually use a struct.

Structs

If you are familiar with Objective C, C++ or C then a struct will sound familiar to you but if you are not then chill, there is no need to worry. I'll try to explain the concept in a precise and easy way.

Structs Fundamentals

What to use:

Structs

Structs are very similar to the cases, but there are several major differences among them too, these are:

  • Structs don't support inheritance.
  • Structs are a value type, whereas classes are reference types.
  • We can't initialize the fields of a struct inside the definition.

The concept of structs is quite easy and simple.

Properties and Attributes

  • Structs are usually small and simple in short.
  • Structs can have properties like a class.
  • Structs can have methods too.

Explanation | Code

Now for an explanation of how it actually works in a real scenario along with its functionality and some other stuff.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
    class Struct
    {
        public struct Point
        {
            private int xCord;
            private int yCord;
            public Point(int x, int y)
            {
                xCord = x;
                yCord = y;
            }
            public int x
            {
                get { return xCord; }
                set { xCord = value; }
            }
            public int y
            {
                get { return xCord; }
                set { xCord = value; }
            }
        }
    }
    class Program
    {
        public static void Main()
        {
            Console.ReadLine();
        }
    }
}

The code above is just a portion of code, not complete code. Now in the code above I declared a keyword struct instead of class and I also declared 2 private members and afterwards a constructor which for the values of x and y respectively.

At last I defined properties for the class x and class y.

Now what we will do next is create their point values, we can do the same thing using one of two methods:

Hello_Word.Struct.Point p1 = new Hello_Word.Struct.Point(100, 100);

So it goes as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
    class Struct
    {
        public struct Point
        {
            private int xCord;
            private int yCord;
            public Point(int x, int y)
            {
                xCord = x;
                yCord = y;
            }
            public int x
            {
                get { return xCord; }
                set { xCord = value; }
            }
            public int y
            {
                get { return xCord; }
                set { xCord = value; }
            }
        }
    }
    class Program
    {
        public static void Main()
        {
            Hello_Word.Struct.Point p1 = new Hello_Word.Struct.Point(100, 100);
            Console.ReadLine();
        }
    }
}

And we can access the same functionality by using:

Hello_Word.Struct.Point p2 = new Hello_Word.Struct.Point();
p2.x = 70;
p2.y = 75;

So it goes as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
    class Struct
    {
        public struct Point
        {
            private int xCord;
            private int yCord;
            public Point(int x, int y)
            {
                xCord = x;
                yCord = y;
            }
            public int x
            {
                get { return xCord; }
                set { xCord = value; }
            }
            public int y
            {
                get { return xCord; }
                set { xCord = value; }
            }
        }
    }
    class Program
    {
        public static void Main()
        {
            /*  Hello_Word.Struct.Point p1 = new Hello_Word.Struct.Point(100, 100); */
            Hello_Word.Struct.Point p2 = new Hello_Word.Struct.Point();
            p2.x = 50;
            p2.y = 75;
            Console.ReadLine();
        }
    }
}

Why to use STRUCT

It is the most important thing or part of thins entire article, where to use it and why to use it instead of class, so it is used for:

  • Reducing code overload
  • For small domains or calculations
  • Where we don't need to use inheritance
  • Prevent your code from overload, overriding and so on
  • Not using a reference type


Recommended Ebook

Programming List with C#

Download Now!
Similar Articles