Wednesday, 31 January 2018

005 C# Structure & Enum

Structure
Structure is a user defined type which is lightweight than a class. Structure instance 
cannot be created. Here are some features of a structure
  • Structured contain member , like fields and methods.
  • An instance of structure cannot be created. 
  • Structure is value type 
  • Interface can be implemented 
  • Structure does not support inheritance, private, protected, public cannot be defined.
  • C# use the keyword struct , to define a structure. 
  • Structure allow nested type. 
During structures initialization, when a new keyword is called , an appropriate structure constructor 
is called to initialize the structure. We do not need to declare constructor separately.  
Here is the example of structure.  
struct Student
{
       private int m_Age;
       private string m_Name;
      public int Age
      {
      get
       {
        return m_Age;
        }
       set
      {
      m_Age = value;
        }
      }
     public string Name
    {
    get
   {
    return m_Name;
   }
   set
   {
    m_Name = value;
   }
    }

}

Student obj = new Student();
obj.Age = 12;
obj.Name = "John";


Console.WriteLine("Age :" + obj.Age);
Console.WriteLine("Name :" + obj.Name);

Here, the structure contain fields and properties. Age,Name can be assign to the structure and get 
the value of the name and age when required. Structure is value type and no instance can be created, 
it is lightweight compare two class. 

Here is a difference between class and structure 

  • Class is reference type but the structured is value type.
  • Class is created on the heap, structure is created on the stack. 
  • Long heavy weight object, persist for long time, required more instance, then class is used. 
Small object, lightweight, little less data passes for small time, then structure is used. 

As we have discussed earlier, we can implement interface on a structure. Here is a example. 
An interface is defined, School. Structure is defined, named as student. The example shows that, 
how the GetAttenDance method is implemented on the structure.

interface School
{
int GetAttenDance();
}

struct Student : School
{
          public int GetAttenDance()
         {
          //implementation ommitted
         return 50;
          }
}

Student obj = new Student();
int att = obj.GetAttenDance();
Console.WriteLine("Attandace :" + att.ToString());

Enum : It is a value type, data type. The full form is enumeration. This content a set all values 
which are constant. Each value are associated with a numeric instant. If we do not assign any value, 
the default first value is 0, then it is increased by 1 for each value. It is value type and created 
on the stack. It comes under the namespace system.enum The default value type of enum is integer, 
using the cast operator, we can cast the value to long, you int. 

Here is the example of a enum type. An enum is declared named days.

enum Days { SunDay = 1, MonDay, TueDay, WedDay, TruDay, FriDay };
int i=(int)Days.TueDay;
When it is called The returns the days number.





No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...