October 7, 2008

[.NET] - Readonly Constants


There are situations where we would decide the value of a constant member at the time of execution. May we also have different constants of different objects of the class. To overcome these shortcomings, C # is amending another known as reading to be used with data members. This amendment seeks to fix the value of using a constructor method, but can not be changed later. Members readonly May be declared as static fields or fields example. When they declared fields example, they may take different values with different objects. Examine the code below:

class Numbers
  {
    public readonly int m;
    public static readonly int n;
    public Numbers (int x)
    {
      m=x;
    }

    static Numbers ()
    {
      n=100;
    }
  }

The value of m is provided at the creation of an object using the constructor with the parameter x. This value remains constant for this object. Remember that the variable is assigned a value of 100, even before the creation of all instances.

No comments: