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.
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.
Display value of myvar1 100, and myvar2 200
ReadOnly Vs Const Keyword
|ReadOnly Keyword||Const Keyword|
|In C#, readonly fields can be created using readonly keyword||In 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 contructor 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.|
- C# | this Keyword
- Static keyword in C#
- Is vs As operator keyword in C#
- LINQ | Let Keyword
- C# | as Operator Keyword
- C# | is Operator Keyword
- C# | finally keyword
- How to implement is functionality without using is keyword in C#
- typeof Operator Keyword in C#
- Invoking an overloaded constructor using this keyword in C#
- Difference between var and dynamic in C#
- Difference between Ref and Out keywords in C#
- Difference between Hashtable and Dictionary in C#
- Difference between SortedList and SortedDictionary in C#
- Difference between Class and Structure in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.