Loading, please wait...

A to Z Full Forms and Acronyms

Structs and Enums in C# | C# Tutorials

Dec 22, 2020 C#, C Sharp, Shibam Dhar, 2040 Views
A struct type is a value type that is typically used to encapsulate small groups of related variables such as the coordinates of rectangle and all.They are basically for light weighted objects. Unlike class, structs in C# are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct.

Structs and Enums in C# | C# Tutorials

A struct type is a value type that is typically used to encapsulate small groups of related variables such as the coordinates of a rectangle and all. They are basically for light-weighted objects. Unlike class, structs in C# are value type than a reference type. It is useful if you have data that is not intended to be modified after the creation of the struct. Structs contain methods, properties, indexers, and so on. It can not contain default constructors.

Example:

using System;  

public struct Rectangle  

{  

    public int width, height;  

 }  

public class TestStructs  

{  

    public static void Main()  

    {  

        Rectangle r = new Rectangle();  

        r.width = 4;  

        r.height = 5;  

        Console.WriteLine("Area of Rectangle is: " + (r.width * r.height));  

    }  

}  

However, structs are always value types, while classes are always referenced types.

Structs are quite similar to classes, but there is are a few important differences between them. Structs can not have a parameterless constructor and structs do not support inheritance. Structs are value types so in order to create an instance of struct we have to pass to it values for each of its properties. This can be done in three ways.

  1. Relying on default values of the properties in the struct.
  2. Passing values for each public property during instantiation.
  3. Using a constructor method on the struct to give default values.

NOTE: To make an immutable struct whose values can only be set at instantiation, we declare it with read only keyword.

Enums in C Sharp

The enum is a keyword that is used to declare an enumeration. Enumeration is a type that consists of a set of named constant called enumerators.

By default the first enumerator has the value of “0” and the value of each successive enumerator is increased by one.

Like:

enum days {Mon, Tue, …}

It is not necessary to follow the general index number to be given to the enums. We can provide different values to the enums too.

Enum Day { Monday, Tuesday = 4, Wednesday…..}

In the above example “Monday” will have index value as 0 whereas “Tuesday” will have index value as 4 and then everything following this will have one value increased index value, which means “Wednesday” will have value 5 for index eventually.

Note: Now, if the data member of the enum member has not been initialized, then its value is set according to rules.

Rules:

  • if it is the first member, then its value is set to 0 otherwise
  • It set out the value which is obtained by adding 1 to the previous value of enum data member

We can change the type of Enum’s data member since it has a default data type. C Sharp has a default data type of integer for enumerators. However, you can change the data type to anything such as bool, long, double, etc.

Enums can be used inside many things,

such as it can be used inside a class

class Program

{

  enum Level

  {

    Low,
    Medium,
    High

  }

  static void Main(string[] args)

  {

    Level myVar = Level.Medium;

    Console.WriteLine(myVar);

  }

}

Can be used inside the switch statement too.

enumLevel

{

  Low,
  Medium,
  High

}

staticvoidMain(string[] args)

{

  Level myVar = Level.Medium;

  switch(myVar)

  {

    case Level.Low:

      Console.WriteLine("Low level");

      break;

    case Level.Medium:

       Console.WriteLine("Medium level");

      break;

    case Level.High:

      Console.WriteLine("High level");

      break;

  }

}

 

A to Z Full Forms and Acronyms

Related Article