Saturday, 18 August 2018

C#: Constructors

Constructors are used to initialise the object at the time of creation.

Syntax
modifier className(arguments){

}

Here modifier is anything like private, public, protected, internal (or) protected internal. As you see constructors don't have any return type and constructor name equals to the class name.

Constructor that don’t has any arguments is called default constructor.
If you don't provide the constructor, then compiler provides the default constructor for your class.

Following is the complete working application.

Program.cs
using System;

public class Employee
{
    private int id;
    private String firstName;
    private String lastName;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

}

class Program
{
    static void printEmployee(Employee emp)
    {
        Console.WriteLine("id :{0}", emp.getId());
        Console.WriteLine("firstName :{0}", emp.getFirstName());
        Console.WriteLine("lastName :{0}", emp.getLastName());
    }

    static void Main(string[] args)
    {
        Employee emp = new Employee();

        emp.setId(1);
        emp.setFirstName("Hari krishna");
        emp.setLastName("Gurram");

        printEmployee(emp);
    }

}

Output
id :1
firstName :Hari krishna
lastName :Gurram

Overloading constructors
We can overload the constructors.
Constructor overloading is a technique, where multiple constructors are defined with different arguments. For example, notify following snippet, it defines 3 constructors.

    public Employee()
    {

    }

    public Employee(int id)
    {
        this.id = id;
    }

    public Employee(int id, String firstName, String lastName)
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

Following is the complete working application,

Program.cs
using System;

public class Employee
{
    private int id;
    private String firstName;
    private String lastName;

    public Employee()
    {

    }

    public Employee(int id)
    {
        this.id = id;
    }

    public Employee(int id, String firstName, String lastName)
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

}

class Program
{
    static void printEmployee(Employee emp)
    {
        Console.WriteLine("id :{0}", emp.getId());
        Console.WriteLine("firstName :{0}", emp.getFirstName());
        Console.WriteLine("lastName :{0}", emp.getLastName());
    }

    static void Main(string[] args)
    {
        Employee emp = new Employee();

        emp.setId(1);
        emp.setFirstName("Hari krishna");
        emp.setLastName("Gurram");

        printEmployee(emp);
    }

}


Output
id :1
firstName :Hari krishna
lastName :Gurram



Previous                                                 Next                                                 Home

No comments:

Post a Comment