Overview Of Classes In C#


In this Article We will Explain about Following types of Concept 
  1.  What is Classes
  2.  Purpose of Class Constructor 
  3. OverLoading Class Constructor
  4. Destructors
What is Classes ?

Class Consists of Data and behavior.Class Data Reperseneted Its by fields and Behavior is Represented by Methods .if you want Create complex Custom types then we can use make Use Classes.What is Mean Complex custom type lets say for Example if i want store number if can do that with integer.if i want store Customer personal information i can use classes. how to do we create class we use Class KeyWord followed by the Name.
  1. public class Student /*Create Class we use class keyword Followd By the name . i am using Student My class Name*/  

Purpose Of Class Constructor

The Purpose Of class Constructor is to initialize class Fields. A class Constructor is Automatically Called when an instance of class is created.

Constructor Do not have return values and always have same of the class .constructors are not mandatory.if we do not provide constructor default parameter less constructor is automatically Provided .Constructors can be overloaded by the number and type of parameters.

Constructor is not mandatory.if we not provide Constructor Class automatically Called Default Constructor.

Constructor Basicllay used Instalize class Fields.Let Say Example of the Code For Your clear Explain,
  1. // Default Constructor  
  2. public Student(): this("No First name Provided"" No Lastname Provided") {}  
  3. // Declaring Parameterized constructor with Parameters  
  4. public Student(string Firstname, string Lastname) {  
  5.     this._firstname = Firstname;  
  6.     this._lastname = Lastname;  
  7. }  


Destructor have the Same name as the class with ~ symbol in front of them.Destructor have Dont take any parameter and do not return value . destructor normally called when the C# Garbage collector Decides to clean your object from memory. class have only on Destructor . we cannot called Destructor its invoke automatically .Destructor Dont have parameter.
  1. /* Destructor*/ ~Student() {  
  2.     //Clean the code   
  3. }  
Finally we see Whole code .how To invoke Class To main class. if want Class invoke Class we need To create Instance of Class we use new Keyword .when create Instance of Class Constructor Automatically called
  1. using ClassSample;  
  2. using System;  
  3. namespace ClassSample {  
  4.     public class Student /*Create Class we use class keyword Followd By the name . i am using Student My class Name*/ {  
  5.         string _firstname;  
  6.         string _lastname;  
  7.         // Default Constructor  
  8.         public Student(): this("No First name Provided"" No Lastname Provided") {}  
  9.         // Declaring Parameterized constructor with Parameters  
  10.         public Student(string Firstname, string Lastname) {  
  11.             this._firstname = Firstname;  
  12.             this._lastname = Lastname;  
  13.         }  
  14.         /*This Method Is Printfulname*/  
  15.         public void printfulname() {  
  16.                 Console.WriteLine("FullName={0}"this._firstname + "" + this._lastname);  
  17.                 Console.ReadLine();  
  18.             }  
  19.             /* Destructor*/  
  20.             ~Student() {  
  21.                 //Clean the code   
  22.             }  
  23.     }  
  24. }  
  25. class Program {  
  26.     public static void Main(string[] args) {  
  27.         /* default constructor parameter Less constructor */  
  28.         Student S2 = new Student();  
  29.         S2.printfulname();  
  30.         /* parameter constructor */  
  31.         Student S1 = new Student("Thenn""arasu"); /* this is called as overloading Constructor */  
  32.         S1.printfulname();  
  33.     }  
  34. }  
the Compile The Run the We can See Two result One Default Constructor And other Parameter Constructor .its showing Console Application,



I hope your Understanding How To use Class ,Constructor and Default Constructor.please share yours valuable feedback and Comments to Improve my Future Article. thanks For Reading My Article.