Are you struggling with Readonly And Constant Members In C#?



If so, Check out my article:

Read-only Field:-


The readonly keyword is  a modifier that you can use 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 construction in the same class.



You can assign a value to a readonly field only in the following way.

  • Variable can be initialized at declaration time.
           Ex. public readonly float val = 52.8f;
  • For an instance field ,in the instance constructors of the class that contains the field declaration.
                                                                              OR
  • For a static field ,in the static constructor of the class that contains the field declaration .
  • You can also pass a readonly field as an out or ref parameter.
    Note:-
  • The Read only field can be initialized either at declaration or in a constructor.Therefore,readonly field can have different values depending on the constructor used.
  • A const field is a compile -time constant ,the readonly can be used for runtime constant as given below.
         public static readonly uint A=(uint) Datetime.Now.Ticks;

Constant Field :-

The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified. A constant declaration introduces one or more constants of a given type. 


Constant Declaration Syntax


[attribute ][modifier] const [type][declarators];
Example:-
public const int x=56;

Note:-
  • Attribute and Modifier is optional.
  • Type can be bytecharshortintlongfloatdoubledecimal, boolstring, an enum type, or a reference type. 
  • The attributes and modifiers apply to all of the members declared by the constant declaration.
  • The type of a constant declaration specifies the type of the members introduced by the declaration. A constant expression must yield a value of the target type, or of a type that can be implicitly converted to the target type. 
  • A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and null.
  • We can declare multiple constants in one line as given below:-
          public const int a=8;b=9;c=5; d=7;
  • The static modifier is not allowed in a constant declaration.
  • constant can participate in a constant expression as given below:-
         public const int a=8;    public const int b=7;    public const int c=a+b+45; 

Example:

class ConstantReadonlyClass
    {
        public const int val1 = 25;
        public readonly float val2 = 20.5f;
        public ConstantReadonlyClass() {
            //val2 = 2.5f;
        }
        public ConstantReadonlyClass(float y)
        {
            val2 = y;
        }
        static void Main(string[] args)
        {
            ConstantReadonlyClass ConstantReadonlyC1 = new ConstantReadonlyClass();
            Console.WriteLine("Val1 = {0} & Val2 = {1}", val1,ConstantReadonlyC1.val2);
            Console.WriteLine("\n-------------\n");
            ConstantReadonlyClass ConstantReadonlyC2 = new ConstantReadonlyClass(3.15f);
            Console.WriteLine("Val1 = {0} & Val2 = {1}", val1, ConstantReadonlyC2.val2);
            Console.ReadKey();
        }
    }


Suggest your output values:

Post a Comment

Thanks for your comments.

Previous Post Next Post