Difference between Const and ReadOnly



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 for const in c#.net



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 for readonly modifier in c#.net



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();

        }

    }

}



Difference between const and readonly

  • const fields has to be initialized while declaration only, while readonly fields can be initialized at declaration or in the constructor.
  • const variables can declared in methods ,while readonly fields cannot be declared in methods.
  • const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
  • A const field is a compile-time constant, the readonly field can be used for run time constants.



Zooming in Winforms Webbrowser Control in .NET
Filtering Datatable with Select Conditions and Sorting in C#.NET
Hiding a property from displaying in Datagridview in C#.NET
Logic to detect if Application is started from Citrix/Terminal Server/PC in C#
Winforms Application.DoEvents Method
Modify XML file with tagname in c#.net
Read Machine IP Address in C#
Check if IIS is running using C#
Implementing IEnumerable in C#
Built in Delegates in .NET Framework
Changing Winform webbrowser control IE version in C#.NET
Set the dropdown width of any combobox based on the data
Example for Property Change Notification in C#.NET
Example Program using Delegates and Events in C#
AutoComplete in .NET Winforms TextBox
How to read data from XML String and insert in to table in SQL Server
WebBrowser.navigate Data Submission by Get or Post Methods
Datagridview Paging
Copy Datagridview cell value using ContextMenu
Inserting data in Datatable in C#
Cookie in .NET
Using Control's Child Index and Dock property for achieving desired layout in a form.
Accessing section group inside configuration of web.config
Create File in .NET C#
Change button shape in .net
Dispose vs Finalize in .net




const vs readonly in c#, static const c#, visual basic const, const property c#, const variable c#, c# consts, c# readonly vs const, const and readonly, readonly vs const, c# readonly keyword, const and readonly, c# readonly vs const, c# read only