Classes In C#

Class

A class consists of the data, fields, and the methods. Let's take an example, and discuss the class and its members.
  1. class Student  
  2.     {  
  3.      //data of a class.  
  4.         string firstName;  
  5.         string lastName;  
  6.   
  7. //Constructor of class.  
  8.   public Student()  
  9.         {  
  10.             
  11.         }  
  12.   
  13. //Method in the class.  
  14.         public void Print()  
  15.         {  
  16.              
  17.         }  
  18.   
  19.  //Destructor   
  20.         ~Student()  
  21.         {  
  22.             firstName = null;  
  23.             lastName = null;  
  24.         }  
  25. }  
As shown in the example above, a class Student is defined and this consists of various members.
  1. DataFields: string firstName and string lastName defines the datafields of Student class. 
    1. public Student() {}  
This defines the constructor of the class. Let's discuss constructors in detail. 

Constructor has the same name as the class, as we have already seen in the example above. The class and constructor are both named Student.

The constructor can accept the parameters, as shown below.
  1.   class Student  
  2.     {  
  3.         //data of a class.  
  4.         string firstName;  
  5.         string lastName;  
  6.   
  7.         //Constructor of Student class with parameters.  
  8.         public Student(string fName, string lName)  
  9.         {  
  10. }  
  11. }  
The constructors are used to initialize the class fields, because the constructor is called by default, when we create an instance of a class, as shown below. 
  1. class Student {  
  2.     //data of a class.    
  3.     string firstName;  
  4.     string lastName;  
  5.     //Constructor of Student class.    
  6.     public Student(string fName, string lName) {  
  7.         //Initialise in constuctor:    
  8.         firstName = fName;  
  9.         lastName = lName;  
  10.         //or we can use by using this keyword because this keyword acts as the instance of the current class.Lets see below:    
  11.     }  
  12. }  
  13. class Program {  
  14.     static void Main(string[] args) {  
  15.         Student student = new Student("Neha""Jangra");  
  16.     }  
  17. }  
When we create an instance of class in the example above and run the program, then we can see by putting a breakpoint the constructor will be called at that point and values will be assigned to the data fields in the constructor only.
 
Please see the attached screenshot.


 
More than one constructor can be defined in a class and we can choose which one needs to be executed from where we create an instance of that class. Let's see it in an example.
  1.    class Student  
  2.     {  
  3.         //data of a class.  
  4.         string firstName;  
  5.         string lastName;  
  6.   
  7.         //Constructor of Student class.  
  8.         public Student(string fName, string lName)  
  9.         {  
  10.             //Initialise in constuctor:  
  11.             firstName = fName;  
  12.             lastName = lName;  
  13.             //or we can use by using this keyword because this keyword acts as the instance of the current class.Lets see below:  
  14.   
  15.         }  
  16.         public Student()  
  17.         {              
  18.         }  
  19. }  
In the case above, when we try to create an instance of the student class mentioned above, then we will get two options for it. Please see the screenshots.



It is not necessary to write a constructor for the class because every class has a default constructor, which is provided by .NET Framework and the default constructor is parameterless.
 
Methods

Methods are the ones that define what our class can logically do; i.e., Print, Save, Update, etc.
  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.   
  6.          // Student student = new Student("Neha", "Jangra");  
  7.           Student student = new Student();// we are able to call the default consturctor.  
  8.           //lets run to see if constuctor gets called.  
  9.           //Call the print method.  
  10.           student.Print();  
  11.       }  
  12.   }  
  13.   //class: A class consists of data,fields and methods.Lets take an example:  
  14.   
  15.   class Student  
  16.   {  
  17.       //data of a class.  
  18.       string firstName;  
  19.       string lastName;  
  20.   
  21.       //Constructor of Student class.  
  22.       public Student(string fName, string lName)  
  23.       {  
  24.           //Initialise in constuctor:  
  25.           firstName = fName;  
  26.           lastName = lName;  
  27.           //or we can use by using this keyword because this keyword acts as the instance of the current class.Lets see below:  
  28.   
  29.       }  
  30.       public Student()  
  31.       {  
  32.           
  33.       }  
  34.       //Method in the class.  
  35.       public void Print()  
  36.       {  
  37.           Console.WriteLine("Student name is {0}"this.firstName + " " + this.lastName);  
  38.       }  
  39. }
Print is a method in the example stated above and to invoke a non static method, we have created an instance of the class.

Destructors
  • The destructors  also have the same name as that of class and are defined using the symbol ~ . 
  • The destructors are used to clean up the resources after use.
  • The destructors are called by the garbage collector itself and we do not need to call them. 
  1. class Student {  
  2.     //data of a class.    
  3.     string firstName;  
  4.     string lastName;  
  5.     //Constructor of Student class.    
  6.     public Student() {}  
  7.         //Destructor of Student class.    
  8.         ~Student() {  
  9.             //Cleanup code here.    
  10.             firstName = null;  
  11.             lastName = null;  
  12.         }  
  13. }  
Thank you. 
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now