Constructors

Constructor #

Constructors are methods with the same name as the class.

They are used to initialize the state of an object and they works with the new keyword.


Post class has a constructor that takes two parameters: title and description

public class Post
{
    private string title;
    private string description;
    
    public Post(string title, string description)
    {
        this.title = title;
        this.description = description;
    }

    public void Show()
    {
        Console.WriteLine(title);
        Console.WriteLine("-----------------");
        Console.WriteLine(description);
    }
}

Let’s create an object of Post class with initial values for title and description.

var intro = new Post("Introduction", "This is my first post");
intro.Show();
// output:
// Introduction
// -----------------
// This is my first post

Class can have multiple constructors if the signature of the methods are different each other.

public class Post
{
    private string title;
    private string description;

    public Post()
    {
        this.title = "Empty";
        this.description = "Empty";
    }

    public Post(string title)
    {
        this.title = title;
        this.description = "Empty";
    }
    
    public Post(string title, string description)
    {
        this.title = title;
        this.description = description;
    }
}