Difference between readonly and const keyword in C# Last Updated : 12 May, 2021 Comments Improve Suggest changes Like Article Like Report In C#, a const keyword is used to declare constant fields and constant local. The value of the constant field is the same throughout the program or in other words, once the constant field is assigned the value of this field is not be changed. In C#, constant fields and locals are not variables, a constant is a number, string, null reference, boolean values.Example: CSharp // C# program to illustrate the // use of const keyword using System; class GFG { // Constant fields public const int myvar = 10; public const string str = "GeeksforGeeks"; // Main method static public void Main() { // Display the value of Constant fields Console.WriteLine("The value of myvar: {0}", myvar); Console.WriteLine("The value of str: {0}", str); } } Output: The value of myvar: 10 The value of str: GeeksforGeeks In C#, you can use a readonly keyword to declare a readonly variable. This readonly keyword shows that you can assign the variable only when you declare a variable or in a constructor of the same class in which it is declared.Example: CSharp // C# program to illustrate the use // of the readonly keyword using System; class GFG { // readonly variables public readonly int myvar1; public readonly int myvar2; // Values of the readonly // variables are assigned // Using constructor public GFG(int b, int c) { myvar1 = b; myvar2 = c; Console.WriteLine("Display value of myvar1 {0}, "+ "and myvar2 {1}", myvar1, myvar2); } // Main method static public void Main() { GFG obj1 = new GFG(100, 200); } } Output: Display value of myvar1 100, and myvar2 200 ReadOnly Vs Const Keyword ReadOnly KeywordConst KeywordIn C#, readonly fields can be created using readonly keywordIn C#, constant fields are created using const keyword.ReadOnly is a runtime constant.Const is a compile time constant.The value of readonly field can be changed.The value of the const field can not be changed.It cannot be declared inside the method.It can be declared inside the method.In readonly fields, we can assign values in declaration and in the constructor part.In const fields, we can only assign values in declaration part.It can be used with static modifiers.It cannot be used with static modifiers. Comment More infoAdvertise with us Next Article Difference between readonly and const keyword in C# A ankita_saini Follow Improve Article Tags : C# CSharp-keyword Similar Reads Difference between Console.Read and Console.ReadLine in C# In C#, to take input from the standard input device, the following method are used - Console.Read() and Console.ReadLine() method. Console is a predefined class of System namespace. While Read() and ReadLine() both are the Console Class methods. The only difference between the Read() and ReadLine() 2 min read Difference between Class and Structure in C# In C#, both classes and structures are used to define custom data types, but there are some differences between them. Inheritance: A class can inherit from other classes, but a structure cannot. In other words, a class can be derived from another class, but a structure cannot be. Reference type vs V 5 min read Difference between Console.Write and Console.WriteLine in C# In C#, to print the data on the console output screen the following method are used - Console.Write() and Console.WriteLine() method. Console is a predefined class of System namespace. While Write() and WriteLine() both are the Console Class methods. The only difference between the Write() and Write 1 min read Difference between String and string in C# String is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class and is defined in the .NET base class library. The size of the String object in memory is 2GB and this is an immutable object, we can not modify the charact 2 min read Difference between Managed and Unmanaged code in .NET Managed code is the code which is managed by the CLR(Common Language Runtime) in .NET Framework. Whereas the Unmanaged code is the code which is directly executed by the operating system. Below are some important differences between the Managed code and Unmanaged code: Managed CodeUnmanaged CodeIt i 2 min read Like