What is the difference between const
and readonly
in C#?
When would you use one over the other?
when to use const
or readonly
const
readonly
App.config
, but once it initializes it can't be changedOne thing to add to what people have said above. If you have an assembly containing a readonly value (e.g. readonly MaxFooCount = 4; ), you can change the value that calling assemblies see by shipping a new version of that assembly with a different value (e.g. readonly MaxFooCount = 5;)
But with a const, it would be folded into the caller's code when the caller was compiled.
If you've reached this level of C# proficiency, you are ready for Bill Wagner's book, Effective C#: 50 Specific Ways to Improve Your C# Which answers this question in detail, (and 49 other things).
The key difference is that Const is the C equivalent of #DEFINE. The number literally gets substituted a-la precompiler. Readonly is actually treated as a variable.
This distinction is especially relevant when you have Project A depending on a Public constant from Project B. Suppose the public constant changes. Now your choice of const/readonly will impact the behavior on project A:
Const: project A does not catch the new value (unless it is recompiled with the new const, of course) because it was compiled with the constants subtituted in.
ReadOnly: Project A will always ask project B for it's variable value, so it will pick up the new value of the public constant in B.
Honestly, I would recommend you use readonly for nearly everything except truly universal constants ( e.g. Pi, Inches_To_Centimeters). For anything that could possibly change, I say use readonly.
Hope this helps, Alan.
The difference is that the value of a static readonly field is set at run time, so it can have a different value for different executions of the program. However, the value of a const field is set to a compile time constant.
Remember: For reference types, in both cases (static and instance), the readonly modifier only prevents you from assigning a new reference to the field. It specifically does not make immutable the object pointed to by the reference.
For details, please refer to C# Frequently Asked Questions on this topic: http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274791.aspx
A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const
keyword and must be initialized as they are declared.
public class MyClass
{
public const double PI1 = 3.14159;
}
A readonly
member is like a constant in that it represents an unchanging value. The difference is that a readonly
member can be initialized at runtime, in a constructor, as well being able to be initialized as they are declared.
public class MyClass1
{
public readonly double PI2 = 3.14159;
//or
public readonly double PI3;
public MyClass2()
{
PI3 = 3.14159;
}
}
const
static
(they are implicitly static)readonly
One of the team members in our office provided the following guidance on when to use const, static, and readonly:
One final note: a const field is static, but the inverse is not true.