Posts

Showing posts with the label C# | readonly vs const

C# | Readonly vs Const

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: 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)...