Both const and readonly in C# are used to declare constants, but they have some differences in terms of when and how they can be used.
const: Constants declared using the const keyword are evaluated at compile-time and can only hold simple data types (such as numeric values, characters, strings, etc.). They are implicitly static and cannot be changed after they are assigned a value. They are always treated as compile-time constants and are replaced by their values during compilation. const values are evaluated and substituted by the compiler, which means they are faster in terms of runtime performance.
readonly: Constants declared using the readonly keyword can hold complex data types, and their values can be assigned at runtime or during instance initialization. They are evaluated at runtime when the containing object is instantiated. readonly fields can be initialized either in the constructor or directly at the declaration. Unlike const, they are not evaluated at compile-time, and their values can be different for different instances of the same class.
Here's an example that demonstrates the difference between const and readonly:
csharp
using System;
class Program
{
const int ConstValue = 5; // Compile-time constant
readonly int ReadonlyValue; // Runtime constant
public Program(int value)
{
ReadonlyValue = value;
}
public static void Main()
{
// Using the const value directly
Console.WriteLine("ConstValue: " + ConstValue);
// Creating an instance with a specific value for ReadonlyValue
Program instance1 = new Program(10);
Console.WriteLine("ReadonlyValue (instance1): " + instance1.ReadonlyValue);
// Creating another instance with a different value for ReadonlyValue
Program instance2 = new Program(20);
Console.WriteLine("ReadonlyValue (instance2): " + instance2.ReadonlyValue);
}
}
In this example, ConstValue is a compile-time constant that holds the same value for all instances of the program. ReadonlyValue, on the other hand, is a runtime constant that can hold different values for different instances of the Program class.
When you run the code, you'll see that the const value remains the same for all instances, while the readonly value can differ based on how each instance is initialized.
const: Constants declared using the const keyword are evaluated at compile-time and can only hold simple data types (such as numeric values, characters, strings, etc.). They are implicitly static and cannot be changed after they are assigned a value. They are always treated as compile-time constants and are replaced by their values during compilation. const values are evaluated and substituted by the compiler, which means they are faster in terms of runtime performance.
readonly: Constants declared using the readonly keyword can hold complex data types, and their values can be assigned at runtime or during instance initialization. They are evaluated at runtime when the containing object is instantiated. readonly fields can be initialized either in the constructor or directly at the declaration. Unlike const, they are not evaluated at compile-time, and their values can be different for different instances of the same class.
Here's an example that demonstrates the difference between const and readonly:
csharp
using System;
class Program
{
const int ConstValue = 5; // Compile-time constant
readonly int ReadonlyValue; // Runtime constant
public Program(int value)
{
ReadonlyValue = value;
}
public static void Main()
{
// Using the const value directly
Console.WriteLine("ConstValue: " + ConstValue);
// Creating an instance with a specific value for ReadonlyValue
Program instance1 = new Program(10);
Console.WriteLine("ReadonlyValue (instance1): " + instance1.ReadonlyValue);
// Creating another instance with a different value for ReadonlyValue
Program instance2 = new Program(20);
Console.WriteLine("ReadonlyValue (instance2): " + instance2.ReadonlyValue);
}
}
In this example, ConstValue is a compile-time constant that holds the same value for all instances of the program. ReadonlyValue, on the other hand, is a runtime constant that can hold different values for different instances of the Program class.
When you run the code, you'll see that the const value remains the same for all instances, while the readonly value can differ based on how each instance is initialized.
Comments
Post a Comment