Monday, 29 January 2018

004 C# Class

C # tutorial 004 Class
Object oriented programming C# is the most preferable language in .Net Framework. 
.Net Framework support class and instance of a class. A class is template of blueprint of an object 
which define the object completely for software development purpose. For example we can define a 
class student . A student required age, gender, name, height, class, address, parents name, roll number,
date of birth. Now if we want to software for school, the above attribute of a student are almost all for 
a school. When we are going to develop a software for school, we will consider this as attribute and 
use when required. The student class will content this attribute to represent a student completely. 
Like this car, boar, employee, athletes many more class can be created. We must remember that a 
class will not represent a particular student rather it will represent an object of a student. This object will
contain the property or attribute for a student. The behavior or property can be define by the member 
of a class. Member means feels, methods, properties of a particular class. Here is example of a class. 

 
public class student
{
          //fields
          string student_name;
          int student_age;
 
        //property
         public string sname
        {
               get
              {
                   return this.sname;
               }
             set
            {
                 this.student_name = value;
           }
       }
       //method
         public void calculate_fees()
         {
        }
}

Here are some points about the class

Fields : The first section of the student class represent the fields. This field maybe kind of variable types 
integer, string, byte etc. This field can have different access modifier private, public, protected. 

Property : Property of a class is to assign value to different variable. If we declare a variable with private 
access modifier, the variable would not be visible to outside of the class, property are required to assign 
this variables. Get set property assign value from outside world to a variable. 

Method : Method part of a class which is responsible for do some work. A Method can calculate student 
pending fees, marks calculation attendance calculation. 

Now we are considering with a calculator class.

public class calculator
{
       //fields
          int a;
          int b;
          //property
          public int _a
         {
          get
          {
             return this._a;
            }
           set
           {
             this.a = value;
            }
          }
 
           //property
           public int _b
            {
           get
           {
            return this._b;
           }
          set
          {
            this.b = value;
          }
           }


           //method
            public int doSum()
           {
           return (a + b);
             }
}

How to use the class
calculator obj = new calculator();
obj._a = 10;
obj._b = 20;
int x = obj.doSum();

The calculator class have two variable which was assign value by property and the calculation was 
done by method to calculate and output to another variable. 


Nested class : A Nested class is a class which is defined within a class. For example a class a 
content another class. The second class is nested class. A class retain it's all feature during nested
to another class. A good example of a nested class is club and club member. A club is a class , club 
member is also class under club. Club object has some attribute and properties and methods and
club member has some attribute properties and method. Both class return the property as per oops 
principles. 

class outerclass
{ 
          class Innerclass
          {
         }
}

class outerclass
{
     class Innerclass
     {
     }
     public Innerclass GetNestedClass()
      {
          Innerclass obj = new Innerclass();
          return obj;
        }
}
Constructor : Constructor is a method which is fast called when an object instance is created.
 An object instance is an process of allocating memory, when this memory allocation is done the 
constructor is called. In VB.Net it is called new keyword. A constructor can be.

  • Parameterized Constructor
  • Overloaded Constructor
  • Private Constructor
  • Copy Constructor
  • Static Constructor
Here is example of constructor. 
public class student
{
      public student()
       {
        }
}
In.Net framework C# , if we do not declare constructor, .Net manage it itself. If you write a
class and do not write the constructor of the class, .Net Framework internally create the constructor
when called for initialization of the class.

Parameterized Constructor :
A constructor can define with many definition, depending upon the parameters and data type,  the constructor will be called. If we do not call constructor, we create an instance of the class, C# will automatically create constructor with blank parameter and initialise accordingly. If we call a class with passing parameter, the appropriate matching parameter constructor will be called. It is possible to declare more than one constructor in a class, and initialise constructor depending about the calling parameters. Here is the example of a parameterized constructor.


public class Student
{
          private int s_Age;
          private string s_Name;
          public Student()
         {
          s_Age = 12;
          s_Name = "John";
         }
        public Student(int age, string name)
        {
         s_Age = age;
         s_Name = name;
        }
}
Student p1 = new Student();
Student p1 = new Student(15,"Rohit");

Overloaded Constructor :

public class Student
{
          private int s_Age;
          private string s_Name;
         public Student()
        {
         s_Age = 12;
        s_Name = "John";
         }
        public Student(int age, string name)
        {
         s_Age = age;
         s_Name = name;
        }
}
Student p1 = new Student();
Student p1 = new Student(15,"Rohit");


Private Constructor :

A constructor can be declared with access modifier private. When we declare constructor with private modifier, the class loose some functionality. A class with private constructor, instance cannot be created for this class. This class will behave like a singleton pattern. In design pattern singleton pattern is a pattern, where an object instance create once  for the whole application. The class cannot be inherited also.


  class Student
  {
  //private constructor
      private Student()
     {
     }
  }
class Student
 {
    static void Main(string[] args)
     {
      Student obj = new Student();//Error cannot access private constructor
    }
}

Copy Constructor :

As we know a construct a special is a method which is called fast during the instance creation of a class. After the instance of a class is created, this instant use to initialize another class by copying the class. When pass, the object instance of the first class into the second class through parameter. here
 is the example of copy constructor.


           public class Student
          {
                   private int s_Age;
                   private string s_Name;
                   public Student()
                  {
                   s_Age = 12;
                   s_Name = "John";
                 }
                  public Student(int age, string name)
                  {
                  s_Age = age;
                  s_Name = name;
               }
                //copy constructor
                 public Student(Student obj)
                {
                  s_Age = obj.s_Age;
                 s_Name = obj.s_Name;
               }
   }

/ Instance constructor.
Student p1 = new Student(12,"John");

// Copy Constructor
Student p2 = new Student(p1);

Static Constructor : Static constructor is a special type of constructor which is used to initialize static data during instance creation of a class. When member of a class static, then static constructor is created. Which is either data member of function of a class static, static constructor is used. Static constructor cannot be call directly. It is called before the main constructor is called. When a constructor is declared static, the whole class loose inheritance functionality.
 
class Student
{
         static int s_Age;
        static Student()
        {
              Console.WriteLine("Static Constructor Executed");
              s_Age = 12;
          }
        private int ShowData()
       {
         Console.WriteLine("A = " + A);
        }

      static void Main(string[] args)
      {
      Student P1 = new Student(20);
     Console.Write(P1.ShowData());
    //10
     }
}

Destructor : Destructor is a method which is the last method of the class. It is declared as the same name of the class name. In general purpose software engineer no need to call destructor of a class,.Net automatically call destructor of the objects when instance have no use. It is a part of a garbage collection and memory management technique of. Net framework. .Net automatically call the cleanup command to clean and destroy of an object instance, software engineer cannot handle or cannot invoke the cleanup command of. Net framework. We will learn in details in the letter chapter.

class Student
{
      public Student()
      {
        Console.WriteLine("Constructor");
       }
     ~Student()
     {
    Console.WriteLine("Destructor");
     }
}


class Student
{
      static void Main()
      {
        Student x = new Student();
       }
}


//Output
//Constructor
//Destructor

Some More about class 

Static Class: When a class have when is static member, it may be data member or maybe Member function, it is called static class. As like static constructor class instance cannot be created. That a static class is created to make some direct Access to the data member and function. Static class also loose its its functionality inheritance. During instance creation of a static class , the static constructor is called fast, then the original constructor is called. If during the static constructor initialization , error occurred ,  error cannot be handle as it does not throw any exception. Example of static class

 
public static class School
 {
    public static int CalculateArea(int width, int height)
    {
    return width * height;
    }
}
Singleton Class :
During singleton Design pattern , it is necessary to create an object instance in such a way that no duplicate object instance can not be created of the same type. Singleton Design pattern can be achieved by declaring constructor private and declaring static variable to consume the object instance of the same class. Singleton class has only a single entry to the class. No other instance can be created and access of the member .


public sealed class Student
{
     private static readonly Student instance = new Student();
    static Student()
    {
    }
   private Student()
   {
   }
   public static Student Instance
   {
       get
         {
            return instance;
         }
   }
}
Sealed Class : A class will lose is inheritance functionality , when we declare the class as sealed. This class cannot be used as base class, no child class cannot be defined of it. Here is the example of Sealed class.


class School
{
         virtual public void printSchool()
         {
          Console.WriteLine("School");
           }
}


class Student : School
{
              sealed override public void printStudent()
               {
               Console.WriteLine("Student");
               }
}


class Class : Student
{
//Error: cannot override inherited member
//Student.printStudent because it is sealed.
}


Parcial Class :
Partial class as the define, class split into many parts. For example class is defined in a place a, the class is defined again in a place b again. When the software Run, the class act as a whole, from place a and please b,  work as a whole class. If the class is declared as sealed class in any place, and the least part of the class are define in other place, the whole class will act as a sealed class. Partial class can be nested. That means definition of class and its nested class can be separate part of the project, during run time will act as a whole class and nested class. Here is the example of partial class.



public partial class Student
{
        public Student()
        {
        }
       public void CalculateAttandance(int val)
        {
        Console.WriteLine(val);
        }
}
public partial class Student
{
          public void CalculateFees(int val)
         {
          Console.WriteLine(val);
         }
}


public partial class Student
{
      public Student()
       {
       }
      public void CalculateAttandance(int val)
        {
         Console.WriteLine(val);
         }
       public void CalculateFees(int val)
       {
        Console.WriteLine(val);
        }
}
Internal Class : An internal class is a class only visible through the namespace. Internal access modifier basic functionality of a class in .Net framework. Internal access modifier is used to hide a class from outside namespace class and assembly. Class may be visible only in the namespace where it was designed for.  


Static Member :Static main is a class which Run when a program start. It is the main entry point of a program. It is the main method of a program.
 

No comments:

Post a Comment

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

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