C# Interview Questions
C# Programming
Web DevelopmentFrontendBackendGame DevQuestion 15
What is the difference between const and readonly?
Answer:
In C#, both const
and readonly
are used to define immutable values, but they serve different purposes and have different behaviors. Understanding these differences is important for using them effectively in your code.
const
Keyword
-
Compile-time Constant:
- The value of a
const
field is set at compile time and cannot be changed thereafter. const
fields are implicitly static and must be initialized with a constant expression.
- The value of a
-
Initialization:
- Must be initialized at the time of declaration.
- Cannot be assigned a value in a constructor or elsewhere in the code.
-
Scope:
const
fields are implicitly static, so they belong to the type itself, not to instances of the type.
-
Usage:
- Typically used for defining constant values that are known at compile time, such as mathematical constants, configuration values, etc.
-
Type:
- Can be used with built-in types (
int
,float
,double
,char
,string
, etc.) andenum
.
- Can be used with built-in types (
Example:
public class Constants
{
public const int MaxValue = 100;
public const string Greeting = "Hello, World!";
}
readonly
Keyword
-
Run-time Constant:
- The value of a
readonly
field can be set at runtime, but only in the constructor of the class. - Once assigned, the value cannot be changed.
- The value of a
-
Initialization:
- Can be initialized at the time of declaration or in the constructor.
- Allows for more flexible initialization, especially when the value is not known until runtime.
-
Scope:
readonly
fields can be instance-specific or static. If declared as static, they belong to the type itself; otherwise, they belong to the instance.
-
Usage:
- Used when the value is constant for the lifetime of the object but might be different for different instances, or when the value needs to be calculated or determined at runtime.
-
Type:
- Can be used with any type, including complex types and objects.
Example:
public class Configuration
{
public readonly int MaxItems;
public readonly string AppName;
public Configuration(int maxItems, string appName)
{
MaxItems = maxItems;
AppName = appName;
}
}
// Usage
var config = new Configuration(100, "MyApp");
Console.WriteLine(config.MaxItems); // Outputs: 100
Console.WriteLine(config.AppName); // Outputs: MyApp
Summary of Differences
Feature | const |
readonly |
---|---|---|
Value Set | At compile time | At runtime (constructor) |
Mutability | Immutable | Immutable after construction |
Initialization | At declaration | At declaration or in constructor |
Scope | Implicitly static | Can be instance-specific or static |
Types Allowed | Primitive types, enum , string |
Any type |
Flexibility | Less flexible, value must be known at compile time | More flexible, value can be calculated at runtime |
When to Use Each
-
Use
const
when:- You have a value that is known at compile time and will never change.
- You need to define constants for configuration, mathematical values, or fixed data.
-
Use
readonly
when:- You need to set the value at runtime, possibly based on constructor parameters.
- The value is immutable once set, but might differ between instances or needs to be computed.
By understanding these differences, you can make informed decisions about when to use const
and readonly
in your C# applications to ensure immutability and proper initialization of your fields.