コード例 #1
0
    static void Main()
    {
        ReadOnlyTest rot = new ReadOnlyTest();

        Console.WriteLine(rot.field_1);
        Console.WriteLine(rot.field_2);
    }
コード例 #2
0
    static readonly int field_2 = 25; // now it's a class-wide constant

    static void Main()
    {
        ReadOnlyTest rot = new ReadOnlyTest();

        Console.WriteLine(rot.field_1);
        Console.WriteLine(ReadOnlyTest.field_2); // access via class name
        Console.WriteLine(field_2);              // or directly because it is static!
    }
コード例 #3
0
    static void Main()
    {
        ReadOnlyTest rot = new ReadOnlyTest();

        Console.WriteLine(rot.field_1);
        Console.WriteLine(rot.field_2);

        rot.field_1 = 2;
        rot.field_2 = 26; // this will cause an error

        Console.WriteLine(rot.field_1);
        Console.WriteLine(rot.field_2);
    }
コード例 #4
0
  public void Execute()
  {
      //Read only 
      //The readonly keyword is a modifier that you can be used on fields. When a field declaration includes a readonly modifier, 
      //assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.
      //The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be 
      //initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. 
      //Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants
      ReadOnlyTest p1 = new ReadOnlyTest(11, 21, 32);   // OK
      Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z);
      ReadOnlyTest p2 = new ReadOnlyTest();
      p2.x = 55;   // OK
     // p2.y = 10; //Not OK
      Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z);
      /*
  Output:
     p1: x=11, y=21, z=32
     p2: x=55, y=25, z=24
 */
  }
コード例 #5
0
    public static void Main()
    {
        ReadOnlyTest rt = new ReadOnlyTest();

        Console.WriteLine(ReadOnlyTest.DATE_CONSTANT);
    }