A class consists of the data, fields, and the methods. Let's take an example, and discuss the class and its members.
- class Student
- {
- //data of a class.
- string firstName;
- string lastName;
- //Constructor of class.
- public Student()
- {
- }
- //Method in the class.
- public void Print()
- {
- }
- //Destructor
- ~Student()
- {
- firstName = null;
- lastName = null;
- }
- }
- DataFields: string firstName and string lastName defines the datafields of Student class.
- 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.
- class Student
- {
- //data of a class.
- string firstName;
- string lastName;
- //Constructor of Student class with parameters.
- public Student(string fName, string lName)
- {
- }
- }
- class Student {
- //data of a class.
- string firstName;
- string lastName;
- //Constructor of Student class.
- public Student(string fName, string lName) {
- //Initialise in constuctor:
- firstName = fName;
- lastName = lName;
- //or we can use by using this keyword because this keyword acts as the instance of the current class.Lets see below:
- }
- }
- class Program {
- static void Main(string[] args) {
- Student student = new Student("Neha", "Jangra");
- }
- }
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.
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.


- class Student
- {
- //data of a class.
- string firstName;
- string lastName;
- //Constructor of Student class.
- public Student(string fName, string lName)
- {
- //Initialise in constuctor:
- firstName = fName;
- lastName = lName;
- //or we can use by using this keyword because this keyword acts as the instance of the current class.Lets see below:
- }
- public Student()
- {
- }
- }


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.
Print is a method in the example stated above and to invoke a non static method, we have created an instance of the class.
Methods are the ones that define what our class can logically do; i.e., Print, Save, Update, etc.
- class Program
- {
- static void Main(string[] args)
- {
- // Student student = new Student("Neha", "Jangra");
- Student student = new Student();// we are able to call the default consturctor.
- //lets run to see if constuctor gets called.
- //Call the print method.
- student.Print();
- }
- }
- //class: A class consists of data,fields and methods.Lets take an example:
- class Student
- {
- //data of a class.
- string firstName;
- string lastName;
- //Constructor of Student class.
- public Student(string fName, string lName)
- {
- //Initialise in constuctor:
- firstName = fName;
- lastName = lName;
- //or we can use by using this keyword because this keyword acts as the instance of the current class.Lets see below:
- }
- public Student()
- {
- }
- //Method in the class.
- public void Print()
- {
- Console.WriteLine("Student name is {0}", this.firstName + " " + this.lastName);
- }
- }
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.
- class Student {
- //data of a class.
- string firstName;
- string lastName;
- //Constructor of Student class.
- public Student() {}
- //Destructor of Student class.
- ~Student() {
- //Cleanup code here.
- firstName = null;
- lastName = null;
- }
- }
Thank you.


NehaPosted Jan 10, 2017, 5:56 PM EST
Thank you so much Gagan :)
gagandeep singhPosted Jan 9, 2017, 3:46 AM EST
This is what i was looking for ... thanks neha for this blog.
Sagar ChowdhuryPosted Jan 8, 2017, 9:46 AM EST
This is really a good explanation .
Jasbeer SinghPosted Jan 4, 2017, 2:25 AM EST
Welcome ............................................................
Jasbeer SinghPosted Jan 4, 2017, 2:06 AM EST
Nice one.................................................................