LEARN c-# VALUE TYPE , STRUCT TYPE , AND ENUMERATION TYPE ( CLASS 3 )

value type :

value type are allow you to store the data directly into the variable. They are derived from SYSTEM.VALUETYPE and have a special behavior in Common Language Run-time ( CLR). The value type and their content are stored same location in memory. The default value of value types are stored in stack. int

struct type:

structs types are a special of objects having the properties of value types. As you know that the value types are stored on the stack; inherently the struct types are also stored on the stack.
                                                                       The structs type encapsulate small group of related variable , such as , the name , rollno , age , class name , and section of a student. They can contain constructor , method , properties , operator , event , nested types , indexers , constant and field. While creating the struct type , you should remember that struct members can not be declared as protected as they fail to support inheritance. Under the struct declaration , the field can be initialized only when they are declared as const or static.

the following code shows how to create "struct type":

public struct student 
{
  string name;
  int rollno;
  int classname;                       // fields
  int section;
.........        // method
............ , , // properties
 }
enumeration type:

Enumeration are the user-defined integer data type that are declared using the enum keyword. Using enumeration , you can defined a set of named integral that can be assigned to a variable.
for example :
                         Traffic light have three possible states -- the green light indicating go , the yellow light indicating to get prepared to stop , and the red light to indicating to stop. A traffic light have one of these three states at a time , implying that the traffic light can be only  red , yellow , and green at a time. In such case , you can defined enumeration type variable and assign three value in it.

the following code shows how to create enumeration type :

enum trafficlight
{ red , green , yellow };

to use enumeration type you need to declared a variable of new enumeration type , as showing in the code :

trafficlight t1;

the trafficlight enumeration can take any of the three values , red, green, and yellow.

t1 = trafficlight.red
t1 = trafficlight.green
t1 = trafficlight.yellow

Enumeration by default , being with the value of 0 for the first entry. However you can modify this value by entering a different value for the first entry , as shown in the code:

enum trafficlight
{
  red = 1, green , yellow
}

showing the code for how to use enumeration in C-sharp application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace enumeration.cs
{

    public enum color { red = 1, green, yellow };
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("plese select 1 for red 2 for green 3 for yello");
            string str = Console.ReadLine();
            int colInt = Int32.Parse(str);
            color col = (color)colInt;
            switch (col)
            {
                case color.red:
                    {
                        Console.WriteLine("the selected color is red..");
                    }
                    break;
                case color.green:
                    {
                        Console.WriteLine("the selected color is green..");
                    }
                    break;
                case color.yellow:
                    {
                        Console.WriteLine("the selected color is yellow..");
                    }
                    break;
                default:
                    {
                        Console.WriteLine("invalid option..");
                    }
                    break;
            }
            Console.ReadKey();
        }
    }

}





  

0 comments: