Class
A class consists of the data, fields, and the methods. Let's take an example, and discuss the class and its members.
- class Student
- {
-
- string firstName;
- string lastName;
-
-
- public Student()
- {
-
- }
-
-
- public void Print()
- {
-
- }
-
-
- ~Student()
- {
- firstName = null;
- lastName = null;
- }
- }
As shown in the example above, a class Student is defined and this consists of various members.
- DataFields: string firstName and string lastName defines the datafields of Student class.
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
- {
-
- string firstName;
- string lastName;
-
-
- public Student(string fName, string lName)
- {
- }
- }
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.
- class Student {
-
- string firstName;
- string lastName;
-
- public Student(string fName, string lName) {
-
- firstName = fName;
- lastName = lName;
-
- }
- }
- 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.
- class Student
- {
-
- string firstName;
- string lastName;
-
-
- public Student(string fName, string lName)
- {
-
- firstName = fName;
- lastName = lName;
-
-
- }
- public Student()
- {
- }
- }
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.
- class Program
- {
- static void Main(string[] args)
- {
-
-
- Student student = new Student();
-
-
- student.Print();
- }
- }
-
-
- class Student
- {
-
- string firstName;
- string lastName;
-
-
- public Student(string fName, string lName)
- {
-
- firstName = fName;
- lastName = lName;
-
-
- }
- public Student()
- {
-
- }
-
- public void Print()
- {
- Console.WriteLine("Student name is {0}", this.firstName + " " + this.lastName);
- }
- }
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.
- class Student {
-
- string firstName;
- string lastName;
-
- public Student() {}
-
- ~Student() {
-
- firstName = null;
- lastName = null;
- }
- }
Thank you.