Read Only Properties


Removing Set

To create a read-only property you can exclude the set altogether.

public class Animal 
{
   private int _Age;

   public int Age
   {
      get { return this._Age; }
   }
}

Private Set

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
   {
      get { return this._Age; }
      private set { this._Age = value; }
   }
}


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