| 1. |
What is the difference between constant and readonly in C#? |
|
Answer» A const keyword in C# is used to declare a constant field throughout the program. That means once a variable has been declared const, its value cannot be changed throughout the program. On the other hand, with readonly keyword, you can assign the variable only when it is declared or in a constructor of the same class in which it is declared. Ex: public readonly int xvar1; public readonly int yvar2; // Values of the readonly // variables are assigned // Using constructor public IB(int b, int c) { xvar1 = b; yvar2 = c; Console.WriteLine("The value of xvar1 {0}, "+ "and yvar2 {1}", xvar1, yvar2); } // Main method static public void Main() { IB obj1 = new IB(50, 60); }}Output:The value of xvar1 is 50, and yvar2 is 60Constants are static by default while readonly should have a value assigned when the constructor is declared. |
|