Is there a difference between having a private const
variable or a private static readonly
variable in C# (other than having to assign the const
a compile-time expression)?
Since they are both private, there is no linking with other libraries. So would it make any difference? Can it make a performance difference for example? Interned strings? Anything similar?
The first, const, is initialized during compile-time and the latter, readonly, initialized is by the latest run-time. The second difference is that readonly can only be initialized at the class-level. Another important difference is that const variables can be referenced through "ClassName.
const field value cannot be changed after declaration. readonly fields cannot be defined within a method. const fields can be declared within a method. readonly variables are declared as instance variable and assigned values in constructor.
Readonly variable cannot be modified at run-time. It can only be initialized or changed in the constructor. Constant variables cannot be modified after declaration. Static members can be accessed using ClassName.
Constant and ReadOnly keyword is used to make a field constant which value cannot be modified. The static keyword is used to make members static that can be shared by all the class objects.
Well, you can use consts in attributes, since they exist as compile time. You can't predict the value of a static readonly variable, since the .cctor
could initialize it from configuration etc.
In terms of usage, constants are burnt into the calling code. This means that if you recompile a library dll to change a public constant, but don't change the consumers, then he consumers will still use the original value. With a readonly variable this won't happen. The flip of this is that constants are (very, very slightly) quicker, since it simply loads the value (rather than having to de-reference it).
Re interning; although you can do this manually, this is most commonly a compiler/runtime feature of literals; if you init a readonly field via a literal:
someField = "abc";
then the "abc"
will be interned. If you read it from config, it won't be. Because a constant string must be a literal, it will also be interned, but it is accessed differently: again, reading from the field is a de-reference, rather than a ldstr
.
Indeed, the two types cannot be changed after they were initialized, but there are some differences between them:
For example const could be used in this situation:
public class MathValues
{
public const double PI = 3.14159;
}
And readonly would be better for this case:
public class Person
{
public readonly DateTime birthDate;
public Person(DateTime birthDate)
{
this.birthDate = birthDate;
}
}
or
public class Person
{
public readonly DateTime birthDate = new DateTime(1986, 1, 24);
}
'const' is static, so it is shared between all instances of that class and can be accessed directly (like MathValues.PI), whereas 'readonly' is not static. As a consequence a declaration like 'static const' is illegal because const is static, but 'static readonly' is legal
'const' can hold only integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration, or a reference to null (not classes or structures because they are initialized at runtime, with the 'new' keyword), whereas 'readonly' can hold complex types, structures or classes (by using the new keyword at initialization) but cannot hold enumerations
Here are the differences between C# .NET const, readonly and static readonly fields (from this article).
Constants:
Readonly instance fields:
Static readonly fields:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With