Readonly and const : readonly « Class « C# / CSharp Tutorial






public class MyClass
{
   public MyClass()
   {
      this.y = 4;
      this.y = 6;

      SetField( ref this.y );
   }

   private void SetField( ref int val )
   {
      val = 8;
   }

   public readonly int x = 123;
   public readonly int y;
   public const     int z = 555;

}

public class MainClass{
   static void Main()
   {
      MyClass obj = new MyClass();

      System.Console.WriteLine( "x = {0}, y = {1}, z = {2}",
                                obj.x, obj.y, MyClass.z );
   }

}
x = 123, y = 8, z = 555








7.36.readonly
7.36.1.Use readonly
7.36.2.Readonly Fields
7.36.3.static readonly fields
7.36.4.Readonly and const
7.36.5.Creating a read-only property.