Saturday 18 August 2018

C#: getter and setter properties

C# provides ‘get’, ‘set’ and ’value’ keywords to generate getters and setters. Let me explain with an example.

Program.cs
using System;

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 Main(string[] args)
    {
        Employee emp1 = new Employee();
        emp1.setId(1);
        emp1.setFirstName("Hari Krishna");
        emp1.setLastName("Gurram");

        Console.WriteLine("Id : {0}", emp1.getId());
        Console.WriteLine("First Name : {0}", emp1.getFirstName());
        Console.WriteLine("Last Name : {0}", emp1.getLastName());
    }
}

Output
Id : 1
First Name : Hari Krishna
Last Name : Gurram

Notify Employee class, I added getters and setters manually. Same employee class rewritten using ‘get’, ‘set’ and ‘value’ keywords like below.


For example, getters and setters for the property id are written like below.
public int Id
    {
        get
        {
            return this.id;
        }

        set
        {
            this.id = value;
        }

    }

'value' keyword is used to set the value to the property id.

You can set the value of the property id like below.
emp1.Id = 1;

Following is the complete working application by using get, set and value keywords.

Program.cs
using System;

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

    public int Id
    {
        get
        {
            return this.id;
        }

        set
        {
            this.id = value;
        }

    }

    public String FirstName
    {
        get
        {
            return this.firstName;
        }

        set
        {
            this.firstName = value;
        }
    }
  
    public String LastName
    {
        get
        {
            return this.lastName;
        }

        set
        {
            this.lastName = value;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Employee emp1 = new Employee();
        emp1.Id = 1;
        emp1.FirstName = "Hari Krishna";
        emp1.LastName = "Gurram";

        Console.WriteLine("Id : {0}", emp1.Id);
        Console.WriteLine("First Name : {0}", emp1.FirstName);
        Console.WriteLine("Last Name : {0}", emp1.LastName);
    }
}


Output
Id : 1
First Name : Hari Krishna
Last Name : Gurram

Add preconditions to getters and setters
Suppose you want to apply some preconditions to getters and setters like below.
a.   Employee id shouldn’t be < 1
b.   Employee firstName and lastName shouldn’t be null (or) empty.


Employee id shouldn’t be < 1
public int Id
    {
        get
        {
            return this.id;
        }

        set
        {
            if(value < 1)
            {
                throw new Exception("id should be a positive integer");
            }
            this.id = value;
        }

    }


Employee firstName and lastName shouldn’t be null (or) empty.
    public String FirstName
    {
        get
        {
            return this.firstName;
        }

        set
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception("String shouldn't be null (or) empty");
            }
            this.firstName = value;
        }
    }
  
    public String LastName
    {
        get
        {
            return this.lastName;
        }

        set
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception("String shouldn't be null (or) empty");
            }
            this.lastName = value;
        }
    }

Following is the complete working application.


Program.cs
using System;

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

    public int Id
    {
        get
        {
            return this.id;
        }

        set
        {
            if(value < 1)
            {
                throw new Exception("id should be a positive integer");
            }
            this.id = value;
        }

    }

    public String FirstName
    {
        get
        {
            return this.firstName;
        }

        set
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception("String shouldn't be null (or) empty");
            }
            this.firstName = value;
        }
    }
  
    public String LastName
    {
        get
        {
            return this.lastName;
        }

        set
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception("String shouldn't be null (or) empty");
            }
            this.lastName = value;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Employee emp1 = new Employee();
        emp1.Id = -1;
        emp1.FirstName = "Hari Krishna";
        emp1.LastName = "Gurram";

        Console.WriteLine("Id : {0}", emp1.Id);
        Console.WriteLine("First Name : {0}", emp1.FirstName);
        Console.WriteLine("Last Name : {0}", emp1.LastName);
    }
}

Short cut to generate getters and setters
If there is no preconditions on properties, then you can use following snippet to generate setters and getters.

class Employee
{
    public int Id { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
}

Program.cs
using System;

class Employee
{
    public int Id { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Employee emp1 = new Employee();
        emp1.Id = 1;
        emp1.FirstName = "Hari Krishna";
        emp1.LastName = "Gurram";

        Console.WriteLine("Id : {0}", emp1.Id);
        Console.WriteLine("First Name : {0}", emp1.FirstName);
        Console.WriteLine("Last Name : {0}", emp1.LastName);
    }
}


Output
Id : 1
First Name : Hari Krishna
Last Name : Gurram


Previous                                                 Next                                                 Home

No comments:

Post a Comment