Pages

Sunday, 14 June 2015

Constants & ReadOnly

Member Constants

Member constants are like the local constants, except that they’re declared in the class declaration rather than in a method. Same as local constants haracteristics, member constant :
  • Initialized value must be computable at compile time and is usually one of the predefined simple types or an expression composed of them.
  • Can not be assigned after its declaration.
Member constants, however, are more interesting than local constants, in that they act like static values. They’re “visible” to every instance of the class, and they’re available even if there are no instances of the class. Unlike statics, constants do not have their own storage locations and are substituted in by the compiler at compile time in a manner similar to #define values in C and C++.
class circle
{
 private const float PI = 3.416F; //Constants member
 public int radius;                  
 public string color;                
 
 public float getArea()
 {
  return PI * radius * radius;
 }

 public void showCircle()
 {
  Console.WriteLine("radius: " + radius + " color: " + color + " area:" + getArea());
 }
}

class Program
{
 static void Main()
 {
  circle circle1 = new circle() { radius = 20, color = "yellow" } ;
  circle circle2 = new circle() { radius = 5, color = "blue" };

  Console.WriteLine("showing circle1");
  circle1.showCircle();
  Console.WriteLine("showing circle2");
  circle2.showCircle();
  Console.Read();
 }
}

readonly Modifier

A field can be declared with the readonly modifier. The effect is similar to declaring a field as const, in that once the value is set, it cannot be changed. Some different characteristics are:
  • While a const field can only be initialized in the field’s declaration statement, a readonly field can have its value set in any of the following places:
    • The field declaration statement—like a const.
    • Any of the class constructors. If it’s a static field, then it must be done in the static constructor.
  • While the value of a const field must be determinable at compile time, the value of a readonly field can be determined at run time. This additional freedom allows you to set different values under different circumstances or in different constructors.
  • Unlike a const,readonly field can be either an instance field or a static field and has a storage location in memory.
Let's change PI constant to readonly fields, eventhough it's not a proper encapsulation. Just to show you how to create a readonly field.
class circle
{
 private readonly float PI;          //readonly field declaration
 public int radius;                 
 public string color;       

 public circle()
 {
  PI = 3.416F;                    //assign value to PI
 }
 ....
}

static void Main()
{
 circle circle1 = new circle() { radius = 20, color = "yellow" } ;
 circle circle2 = new circle() { radius = 5, color = "blue" };

 Console.WriteLine("showing circle1");
 circle1.showCircle();
 ....
}

0 comments:

Post a Comment