Readonly and const : Readonly : Class C# Examples


C# Examples » Class » Readonly »

 

Readonly and const









    
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  );
      }

}
    
   
  
   



Output

x = 123, y = 8, z = 555


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Readonly