Saturday 28 May 2016

get

Encapsulation allows us to secure date source and data set to external worlds.
Getter:
It is used to get data from variable.
You can modify the represent of the data like uppercase, lowercase or if date time change the date formats.
You can check permission before providing date back to the caller.
Syntax:

class myClass
{
    private string _name;

    public string Name
    {
        get
        {
            return _name;
        }
    }
}
Let' ake an example of getter method to modify enter string before get;

using System;

class Program
{
    static void Main()
    {
        myClass cls = new myClass();
        
        Console.WriteLine("Name: {0}", cls.Name);
    }
}

class myClass
{
    private string _name = "Sample";

    public string Name
    {
        get
        {
            return "Mr." + _name;
        }
    }
}
Output:
Mr. Sample
Press any key to continue…
If you provide only get method to the property then it will be a read only property.
If you provide only set method to the property then it will be a write only property.
Read more about types of property. class properties

you can also visit:  www.mybook-lang.com

No comments:

Post a Comment