Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The difference is that the value of a static readonly field is set at run time, and can thus be modified by the containing class, whereas the value of a const field is set to a compile time constant.
In the static readonly case, the containing class is allowed to modify it only
static readonly is typically used if the type of the field is not allowed in a const declaration, or when the value is not known at compile time.
Instance readonly fields are also allowed.
Remember that 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.
class Program
{
public static readonly Test test = new Test();
static void Main(string[] args)
{
test.Name = "Program";
test = new Test(); // Error: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
}
}
class Test
{
public string Name;
}
On the other hand, if Test were a value type, then assignment to test.Name would be an error.
Anonymous
December 12, 2004
i want explanation.
Anonymous
November 27, 2007
PingBack from http://feeds.maxblog.eu/item_802202.html
Anonymous
July 15, 2008
PingBack from http://ilikeellipses.com/2008/07/16/intent-programming/
Anonymous
September 17, 2008
PingBack from http://blog.f9group.com/technology/c/what-is-the-difference-between-const-and-static-readonly/
Anonymous
October 05, 2009
dIFFERENCE bETWEEN // and / in C# when using url
Anonymous
October 05, 2009
dIFFERENCE bETWEEN // and / in C# when using url
Anonymous
June 01, 2012
Nice article, I've also wrote an article about that context.
It can be found here:
www.shloemi.com/.../csharp-const-constant-explained-by-faq-qa
Anonymous
March 14, 2013
The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants
Anonymous
September 10, 2013
I need practical example which scenario we really need read only variable or class.
Anonymous
April 07, 2015
Hi Senthil,
if you want to see the detail, see this link:
www.advancesharp.com/.../constant-readonly-and-static-in-c-with-real-life-examples
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in