0

I wonder why access to a member should be set-only denying reading of its value. Can anyone make an explanation or any examples of when to use these?

Thanks in advance

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111

1 Answers1

0

if you want to change the member value only, in this case use Write only property(SET)

for Example:

class employee
{
    int id = 1;
    string name = "chandru";
    string dept = "IT";

    public string Name
    {
        get { return name; }
        private set { name = value; }       //Restricted to modify name to outer this calss
    }

    public string DEPT
    {
        set { dept = value; }
    }
    public void display()
    {
        Console.WriteLine("ID: {0} Name: {1} and Dept: {2}", id, name, dept);
    }
}
class Program
{
    static void Main(string[] args)
    {
        employee e = new employee();
        e.DEPT = "CSE";
        Console.WriteLine(e.Name);
        e.display();
      ///  e.Name = "Prakash";        //you cannot modify becoz of Access specifies as private

    }
}

set property uses to change the member value only which is u want in client application...

Note: by my knowledge i updated, please modify my answer if anything's wrong