Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

19 June 2012

C# Readonly fields


Read-only members are values which are set at compile time or at runtime by using constructor, but cannot be modified later
Read-only members are initialized in either at declaration or at constructor. Readonly member can be instance member or static member.

Syntax:
 
public const [data type] [field name]= value; (or)
 
 
public const [data type] [field name];
public Class_name() // Instance constructor
        {
            [field name] = value;
        }
 

In the below example we will see
  •          How to initialize readonly member at the time declaration
  •          Assigning value to readonly member from the instance, parameterized constructor.
  •          How to initialize static readonly member at the time declaration
  •          Assigning value to static readonly member from the static constructor.
  •          What happens if we  try to reassign readonly value other than variable initialization and constructor
Example:
 class ReadOnlyMember
    {
        public readonly double pi=3.14;
        public readonly double root2;
        public readonly int weekDays;
        public static readonly int months = 12;
        public static readonly int yearDays;
        public ReadOnlyMember(// Instance constructor
        {
            root2 = 1.414;
        }
        public ReadOnlyMember(int valweekDays) //parameterized constructor
        {
            weekDays = valweekDays;
        }
        static ReadOnlyMember(//static constructor
        {
            yearDays = 365;
            Console.WriteLine("called static constructor and readonly member yearDays value is {0}", yearDays);
        }
    }
    class ReadOnlyMemberProgram
    {
        public static void Main()
        {
            ReadOnlyMember obj=new ReadOnlyMember();
            Console.WriteLine("readonly field pi intilized at declaration and value is {0}", obj.pi);
            Console.WriteLine("readonly field root2 intilized in constructor and value is {0}", obj.root2);
            //obj.root2 = 5; //Compile Error : A readonly field can not be assigned (except in a constructor or a variable intializer)
            ReadOnlyMember obj1 = new ReadOnlyMember(7);
            Console.WriteLine("readonly field weekDays. value is passed from object creation and value is {0}"obj1.weekDays);
            //obj1.weekDays = 5; //Compile Error
            Console.WriteLine("static readonly field months intilized at declaration and value is {0}",ReadOnlyMember.months);            
            Console.WriteLine("\nObject obj values are obj.pi={0} ,obj.root2={1}, obj.weekDays={2}", obj.pi, obj.root2, obj.weekDays);
            Console.WriteLine("Object obj1 values are obj1.pi={0} ,obj1.root2={1}, obj1.weekDays={2}", obj1.pi, obj1.root2, obj1.weekDays);
            Console.Read();
            
        }
    }
 
Output:


Error:
If you try to re-assign the value of readonly field other than at variable initialization and constructor we will get compile time error as “A readonly field can not be assigned (except in a constructor or a variable intializer)

0 Comments:

Post a Comment