Write Only Properties

When the value of a property can only be changed outside the class it is called a write-only property.
This is rarely seen or used.
There may also be times when you want the client to change a value but not to retrieve it.
This rarely makes sense though


Removing Get

To create a read-only field you can exclude the get altogether.

public class Animal 
{
   private int _Age;

   public int Age
   {
      set { this._Age = value; }
   }
}

Private Get

Alternatively if you still needed to set the value inside the class then you can change it to private.

public class Animal 
{
   private int _Age;

   public int Age
   {
      private get { return this._Age; }
      set { this._Age = value; }
   }
}


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext