top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the Difference between read only and constant variables?

+1 vote
271 views
What is the Difference between read only and constant variables?
posted Mar 9, 2015 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

const in c#

  • You have to initialize const variables while declaration.
  • You cannot reassign a const variable.
  • Compiler evaluates the const variables.
  • The static modifier is not allowed in a constant declaration.
  • A const field of a reference type other than string can only be initialized with null.

Example:

using System;
using System.Text;

namespace ProgramCAll
{

    class ProgramCall
    {
        /*A const field of a reference type other than string
        can only be initialized with null*/
        private const StringBuilder myName = null;

        public static void Main()
        {
            //You have to initilize Const varabiles while declartion
            const int myEmpId = 173524;

            //Valid scenario
            const int myNumber = 10 + 16;

            const string myName = "John";

            // Reassigning a const variable is not allowed.
            //Comment out below code to run the program
            myEmpId = 23456;

            Console.WriteLine(myEmpId);

            Console.Read();
        }
    }
}

readonly in c#

  • readonly fields can be initialized only while declaration or in the constructor.
  • Once you initialize a readonly field, you cannot reassign it.
  • You can use static modifier for readonly fields
  • readonly modifier can be used with reference types
  • readonly modifier can be used only for instance or static fields, you cannot use readonly keyword for variables in the methods.

Example:

using System;
using System.Text;

namespace ProgramCall
{
    class MyClass
    {
        //readonly can only be used with instance or static variables
        public readonly int mynumber = 10;
        public readonly int addnumber = 10 + 15;

        //readonly modifier can be used with static varaibles       
        public static string myEmployer = "ProgramCall.Com";

        //readonly can be used with reference types
        public readonly StringBuilder name = new StringBuilder("John");

        /*  readonly varaible can be intilized in constructor but
             cannot be declared in constructor    */
        public readonly string myName;

        //readonly fields can be initlized in constructor
        public MyClass(string name)
        {
            myName = name;

        }

        private void myMethod()
        {
            /* readonly modifier cannot be used in method level variables
           the below line throws a error. */
            //  readonly int num = 5;
        }

    }

    //main class
    class ProgramCall
    {

        public static void Main()
        {
            MyClass obj = new MyClass("Bill");

            Console.Read();
        }
    }
}
answer Mar 9, 2015 by Amit Kumar Pandey
0 votes

Read only: can assign the values at runtime only.

Constant: will assign the values at compile time only.

We cannot modify the both variable values.

answer Mar 11, 2015 by Shivaranjini
...