Demonstrate readonly : readonly « Class Interface « C# / C Sharp






Demonstrate readonly

Demonstrate readonly
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Demonstrate readonly. 
 
using System; 
 
class MyClass { 
  public static readonly int SIZE = 10; 
} 
 
public class DemoReadOnly { 
  public static void Main() { 
    int[]nums = new int[MyClass.SIZE]; 
 
    for(int i=0; i<MyClass.SIZE; i++) 
      nums[i] = i; 
 
    foreach(int i in nums) 
      Console.Write(i + " "); 
 
    // MyClass.SIZE = 100; // Error!!! can't change 
  } 
}


           
       








Related examples in the same category

1.ReadOnly Fields
2.The use of readonly fieldsThe use of readonly fields
3.Readonly Property