Home » C# Basics » 05 - Object Oriented Programming in C#
5

Constructor

Defines what constructors are and how it is used in OOP

So far, all the methods we have learned will work (execute) only when we call them. If we define a method but do not call it, the code inside the method will not execute. However, there is one method that executes without calling. This method is known as the constructor of the class.

A constructor is a method that has the same name as that of the class. A constructor does not have any return type, not even void. However, a constructor can take arguments.

Constructors are normally used for providing some valid values to the fields of the class. We can create a constructor as:

public class-name(arguments)
{
}

For example, a constructor for our `Person' class can be defined as -

public Person()
{
statements of the constructor;
}

If we do not define a constructor for our class, a default constructor is used to automatically provide valid values to the fields of the class. However, if we choose to define a constructor, the default constructor is replaced by our constructor.

Before moving ahead, let us take a look at the working of methods. Modify the class `Person' to add a method called Talk() as shown below:

public class Person { private string name; private int age; private string gender; public string Name { get {  return name; } set {  name = value; } }
 public string Gender { get {  return gender; } set {  gender = value; } } public int Age { get {  return age; } set {  age = value; } }
 public void Talk() { Console.WriteLine("{0} is talking", this.Name); } }

The Talk() method displays a message. Notice the statement this.name. Here, this.name refers to the value of the name field of the object that will call this method. Hence, if object p1 calls this method, this.name will refer to the value of the name field of the object p1.

To understand this, modify the Main() method in the Program.cs file. After entering the field values using the properties, we call the Talk() method first using object p1 and then using p2.

class Program { static void Main(string[] args) { Person p1 = new Person(); Person p2 = new Person();
Console.WriteLine("Enter name:"); p1.Name = Console.ReadLine();
Console.WriteLine("Enter Gender:"); p1.Gender = Console.ReadLine(); Console.WriteLine("Enter Age:"); p1.Age = int.Parse(Console.ReadLine());
Console.WriteLine("Enter name:"); p2.Name = Console.ReadLine();
Console.WriteLine("Enter Gender:"); p2.Gender = Console.ReadLine();
Console.WriteLine("Enter Age:"); p2.Age = int.Parse(Console.ReadLine()); Console.WriteLine("{0} is {1} years old {2}", p1.Name, p1.Age, p1.Gender); Console.WriteLine("{0} is {1} years old {2}", p2.Name, p2.Age, p2.Gender); p1.Talk(); p2.Talk();
} }

Here is a sample execution of the modified program:

As shown in the figure, when the statement p1.Talk() is executed, the message displayed is - Jack is talking. This is because the value of the name field for object p1 is Jack and hence, in the Talk() method, this.name mens Jack. Similarly, when the p2.Talk() statement executes, we get the message - Lisa is talking.

In this chapter, we have gone through the basics of object-oriented programming. We have studied the concept of class, objects, fields, properties and methods and we now know how to implement these features in our program. In the following lesson, we will learn about some advanced concepts of object-oriented programming.