<p>What is the difference between <strong><code>const</code></strong> and <strong><code>readonly</code></strong> in C#? </p>

<p>When would you use one over the other?</p>

[{'answer_id': 55987, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 4, 'selected': False, 'text': '<p>I believe a <code>const</code> value is the same for all objects (and must be initialized with a literal expression), whereas <code>readonly</code> can be different for each instantiation...</p>\n'}, {'answer_id': 55988, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 6, 'selected': False, 'text': '<p><a href="http://en.csharp-online.net/const,_static_and_readonly" rel="noreferrer">This explains it</a>. Summary: const must be initialized at declaration time, readonly can be initialized on the constructor (and thus have a different value depending on the constructor used).</p>\n\n<p>EDIT: See Gishu\'s gotcha above for the subtle difference</p>\n'}, {'answer_id': 55993, 'author': 'Wheelie', 'author_id': 1131, 'author_profile': 'https://Stackoverflow.com/users/1131', 'pm_score': 5, 'selected': False, 'text': '<p>A const is a compile-time constant whereas readonly allows a value to be calculated at run-time and set in the constructor or field initializer. So, a \'const\' is always constant but \'readonly\' is read-only once it is assigned.</p>\n<p>Eric Lippert of the C# team has <a href="https://learn.microsoft.com/en-us/archive/blogs/ericlippert/immutability-in-c-part-one-kinds-of-immutability" rel="noreferrer">more information</a> on different types of immutability.</p>\n'}, {'answer_id': 56003, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 8, 'selected': False, 'text': "<p>There is a gotcha with consts! If you reference a constant from another assembly, its value will be compiled right into the calling assembly. That way when you update the constant in the referenced assembly it won't change in the calling assembly!</p>\n"}, {'answer_id': 56024, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 12, 'selected': True, 'text': '<p>Apart from the apparent difference of</p>\n<ul>\n<li>having to declare the value at the time of a definition for a <code>const</code> VS <code>readonly</code> values can be computed dynamically but need to be assigned before the constructor exits. After that it is frozen.</li>\n<li><code>const</code>\'s are implicitly <code>static</code>. You use a <code>ClassName.ConstantName</code> notation to access them.</li>\n</ul>\n<p>There is a subtle difference. Consider a class defined in <code>AssemblyA</code>.</p>\n<pre><code>public class Const_V_Readonly\n{\n  public const int I_CONST_VALUE = 2;\n  public readonly int I_RO_VALUE;\n  public Const_V_Readonly()\n  {\n     I_RO_VALUE = 3;\n  }\n}\n</code></pre>\n<p><code>AssemblyB</code> references <code>AssemblyA</code> and uses these values in code. When this is compiled:</p>\n<ul>\n<li>in the case of the <code>const</code> value, it is like a find-replace.  The value 2 is \'baked into\' the <code>AssemblyB</code>\'s IL. This means that if tomorrow I update <code>I_CONST_VALUE</code> to 20, <em><code>AssemblyB</code> would still have 2 till I recompile it</em>.</li>\n<li>in the case of the <code>readonly</code> value, it is like a <code>ref</code> to a memory location. The value is not baked into <code>AssemblyB</code>\'s IL. This means that if the memory location is updated, <code>AssemblyB</code> gets the new value without recompilation. So if <code>I_RO_VALUE</code> is updated to 30, you only need to build <code>AssemblyA</code> and all clients do not need to be recompiled.</li>\n</ul>\n<p>So if you are confident that the value of the constant won\'t change, use a <code>const</code>.</p>\n<pre><code>public const int CM_IN_A_METER = 100;\n</code></pre>\n<p>But if you have a constant that may change (e.g. w.r.t. precision) or when in doubt, use a <code>readonly</code>.</p>\n<pre><code>public readonly float PI = 3.14;\n</code></pre>\n<p><em>Update: Aku needs to get a mention because he pointed this out first. Also I need to plug where I learned this: <a href="https://rads.stackoverflow.com/amzn/click/com/0321245660" rel="nofollow noreferrer" rel="nofollow noreferrer">Effective C# - Bill Wagner</a></em></p>\n'}, {'answer_id': 56051, 'author': 'Wedge', 'author_id': 332, 'author_profile': 'https://Stackoverflow.com/users/332', 'pm_score': 3, 'selected': False, 'text': '<p>Variables marked const are little more than strongly typed #define macros, at compile time const variable references are replaced with inline literal values. As a consequence only certain built-in primitive value types can be used in this way. Variables marked readonly can be set, in a constructor, at run-time and their read-only-ness is enforced during run-time as well. There is some minor performance cost associated with this but it means you can use readonly with any type (even reference types).</p>\n\n<p>Also, const variables are inherently static, whereas readonly variables can be instance specific if desired.</p>\n'}, {'answer_id': 56233, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': False, 'text': "<p>Just to add, <code>readonly</code> for reference types only makes the reference read only not the values. For example:</p>\n<pre><code>public class Const_V_Readonly\n{\n  public const int I_CONST_VALUE = 2;\n  public readonly char[] I_RO_VALUE = new Char[]{'a', 'b', 'c'};\n\n  public UpdateReadonly()\n  {\n     I_RO_VALUE[0] = 'V'; //perfectly legal and will update the value\n     I_RO_VALUE = new char[]{'V'}; //will cause compiler error\n  }\n}\n</code></pre>\n"}, {'answer_id': 62090, 'author': 'Anthony', 'author_id': 5599, 'author_profile': 'https://Stackoverflow.com/users/5599', 'pm_score': 0, 'selected': False, 'text': "<p>One 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;)</p>\n\n<p>But with a const, it would be folded into the caller's code when the caller was compiled.</p>\n\n<p>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#\nWhich answers this question in detail, (and 49 other things).</p>\n"}, {'answer_id': 67192, 'author': 'AlanR', 'author_id': 7311, 'author_profile': 'https://Stackoverflow.com/users/7311', 'pm_score': 0, 'selected': False, 'text': "<p>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.  </p>\n\n<p>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:</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>Hope this helps,\nAlan.</p>\n"}, {'answer_id': 82893, 'author': 'Hallgrim', 'author_id': 15454, 'author_profile': 'https://Stackoverflow.com/users/15454', 'pm_score': 3, 'selected': False, 'text': '<p>They are both constant, but a const is available also at compile time. This means that one aspect of the difference is that you can use const variables as input to attribute constructors, but not readonly variables.</p>\n\n<p>Example:</p>\n\n<pre><code>public static class Text {\n  public const string ConstDescription = "This can be used.";\n  public readonly static string ReadonlyDescription = "Cannot be used.";\n}\n\npublic class Foo \n{\n  [Description(Text.ConstDescription)]\n  public int BarThatBuilds {\n    { get; set; }\n  }\n\n  [Description(Text.ReadOnlyDescription)]\n  public int BarThatDoesNotBuild {\n    { get; set; }\n  }\n}\n</code></pre>\n'}, {'answer_id': 83173, 'author': 'Mark T', 'author_id': 10722, 'author_profile': 'https://Stackoverflow.com/users/10722', 'pm_score': 2, 'selected': False, 'text': '<p>Another <I>gotcha</I>.<P>\nSince const really only works with basic data types, if you want to work with a class, you may feel "forced" to use ReadOnly.  However, beware of the trap!  ReadOnly means that you can not replace the object with another object (you can\'t make it refer to another object).  But any process that has a reference to the object is free to modify the values <B><I>inside</I></B> the object!<P>\nSo don\'t be confused into thinking that ReadOnly implies a user can\'t change things.  There is no simple syntax in C# to prevent an instantiation of a class from having its internal values changed (as far as I know).</p>\n'}, {'answer_id': 164500, 'author': 'Scott Lawrence', 'author_id': 3475, 'author_profile': 'https://Stackoverflow.com/users/3475', 'pm_score': 3, 'selected': False, 'text': '<p>One of the team members in our office provided the following guidance on when to use const, static, and readonly:</p>\n\n<ul>\n<li>Use <strong>const</strong> when you have a variable of a type you can know at runtime (string literal, int, double, enums,...) that you want all instances or consumers of a class to have access to where the value should not change.</li>\n<li>Use <strong>static</strong> when you have data that you want all instances or consumers of a class to have access to where the value can change.</li>\n<li>Use <strong>static readonly</strong> when you have a variable of a type that you cannot know at runtime (objects) that you want all instances or consumers of a class to have access to where the value should not change.</li>\n<li>Use <strong>readonly</strong> when you have an instance level variable you will know at the time of object creation that should not change.</li>\n</ul>\n\n<p>One final note: a const field is static, but the inverse is not true.</p>\n'}, {'answer_id': 217058, 'author': 'Mike Two', 'author_id': 23659, 'author_profile': 'https://Stackoverflow.com/users/23659', 'pm_score': 5, 'selected': False, 'text': '<p>There is a small gotcha with readonly. A readonly field can be set multiple times within the constructor(s). Even if the value is set in two different chained constructors it is still allowed.</p>\n<pre><code>public class Sample {\n    private readonly string ro;\n\n    public Sample() {\n        ro = &quot;set&quot;;\n    }\n\n    public Sample(string value) : this() {\n        ro = value; // this works even though it was set in the no-arg ctor\n    }\n}\n</code></pre>\n'}, {'answer_id': 333728, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 8, 'selected': False, 'text': "<h2>Constants</h2>\n\n<ul>\n<li>Constants are static by default</li>\n<li>They must have a value at compilation-time (you can have e.g. 3.14 * 2, but cannot call methods)</li>\n<li>Could be declared within functions</li>\n<li>Are copied into every assembly that uses them (every assembly gets a local copy of values)</li>\n<li>Can be used in attributes</li>\n</ul>\n\n<h2>Readonly instance fields</h2>\n\n<ul>\n<li>Must have set value, by the time constructor exits</li>\n<li>Are evaluated when instance is created</li>\n</ul>\n\n<h2>Static readonly fields</h2>\n\n<ul>\n<li>Are evaluated when code execution hits class reference (when new instance is created or a static method is executed)</li>\n<li>Must have an evaluated value by the time the static constructor is done</li>\n<li>It's not recommended to put ThreadStaticAttribute on these (static constructors will be executed in one thread only and will set the value for its thread; all other threads will have this value uninitialized)</li>\n</ul>\n"}, {'answer_id': 498685, 'author': 'Chris S', 'author_id': 21574, 'author_profile': 'https://Stackoverflow.com/users/21574', 'pm_score': 4, 'selected': False, 'text': '<p><a href="http://www.anotherchris.net/csharp/const-vs-readonly-in-csharp/" rel="noreferrer">Here\'s another link</a> demonstrating how const isn\'t version safe, or relevant for reference types.</p>\n\n<p><strong>Summary</strong>:</p>\n\n<ul>\n<li>The value of your const property is set at compile time and can\'t change at runtime</li>\n<li>Const can\'t be marked as static - the keyword denotes they are static, unlike readonly fields which can.</li>\n<li>Const can\'t be anything except value (primitive) types</li>\n<li>The readonly keyword marks the field as unchangeable. However the property can be changed inside the constructor of the class</li>\n<li>The readonly only keyword can also be combined with static to make it act in the same way as a const (atleast on the surface). There is a marked difference when you look at the IL between the two</li>\n<li>const fields are marked as "literal" in IL while readonly is "initonly"</li>\n</ul>\n'}, {'answer_id': 1393687, 'author': 'Brett Ryan', 'author_id': 140037, 'author_profile': 'https://Stackoverflow.com/users/140037', 'pm_score': 2, 'selected': False, 'text': '<p>A constant will be compiled into the consumer as a literal value while the static string will serve as a reference to the value defined.</p>\n\n<p>As an exercise, try creating an external library and consume it in a console application, then alter the values in the library and recompile it (without recompiling the consumer program), drop the DLL into the directory and run the EXE manually, you should find that the constant string does not change.</p>\n'}, {'answer_id': 1393708, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 1, 'selected': False, 'text': '<p>Principally; you can assign a value to a static readonly field to a non-constant value at runtime, whereas a const has to be assigned a constant value.</p>\n'}, {'answer_id': 1557937, 'author': 'Greg', 'author_id': 12971, 'author_profile': 'https://Stackoverflow.com/users/12971', 'pm_score': 4, 'selected': False, 'text': '<p>Yet another gotcha: readonly values can be changed by "devious" code via reflection.</p>\n\n<pre><code>var fi = this.GetType()\n             .BaseType\n             .GetField("_someField", \n                       BindingFlags.Instance | BindingFlags.NonPublic);\nfi.SetValue(this, 1);\n</code></pre>\n\n<p><a href="https://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499">Can I change a private readonly inherited field in C# using reflection?</a></p>\n'}, {'answer_id': 5057030, 'author': 'Greg', 'author_id': 431780, 'author_profile': 'https://Stackoverflow.com/users/431780', 'pm_score': 2, 'selected': False, 'text': '<p>A <code>const</code> has to be <strong>hard-coded</strong>, where as <code>readonly</code> can be <strong>set in the constructor</strong> of the class.</p>\n'}, {'answer_id': 6082004, 'author': 'Deepthi', 'author_id': 763996, 'author_profile': 'https://Stackoverflow.com/users/763996', 'pm_score': 6, 'selected': False, 'text': "<p><code>const</code>: Can't be changed anywhere.</p>\n\n<p><code>readonly</code>: This value can only be changed in the constructor. Can't be changed in normal functions.</p>\n"}, {'answer_id': 12458557, 'author': 'Sujit', 'author_id': 792713, 'author_profile': 'https://Stackoverflow.com/users/792713', 'pm_score': 5, 'selected': False, 'text': '<p>A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the <code>const</code> keyword and must be initialized as they are declared.</p>\n\n<pre><code>public class MyClass\n{\n    public const double PI1 = 3.14159;\n}\n</code></pre>\n\n<p>A <code>readonly</code> member is like a constant in that it represents an unchanging value. The difference is that a <code>readonly</code> member can be initialized at runtime, in a constructor, as well being able to be initialized as they are declared.</p>\n\n<pre><code>public class MyClass1\n{\n     public readonly double PI2 = 3.14159;\n\n     //or\n\n     public readonly double PI3;\n\n     public MyClass2()\n     {\n         PI3 = 3.14159;\n     }\n}\n</code></pre>\n\n<p><strong>const</strong></p>\n\n<ul>\n<li>They can not be declared as <code>static</code> (they are implicitly static)</li>\n<li>The value of constant is evaluated at compile time</li>\n<li>constants are initialized at declaration only</li>\n</ul>\n\n<p><strong>readonly</strong></p>\n\n<ul>\n<li>They can be either instance-level or static</li>\n<li>The value is evaluated at run time</li>\n<li>readonly can be initialized in declaration or by code in the constructor</li>\n</ul>\n'}, {'answer_id': 19064906, 'author': 'Ramesh Rajendran', 'author_id': 2218635, 'author_profile': 'https://Stackoverflow.com/users/2218635', 'pm_score': 2, 'selected': False, 'text': '<p>Const and readonly are similar, but they are not exactly the same. A const field is a compile-time constant, meaning that that value can be computed at compile-time. A readonly field enables additional scenarios in which some code must be run during construction of the type. After construction, a readonly field cannot be changed.</p>\n\n<p>For instance, const members can be used to define members like:</p>\n\n<pre><code>struct Test\n{\n    public const double Pi = 3.14;\n    public const int Zero = 0;\n}\n</code></pre>\n\n<p>since values like 3.14 and 0 are compile-time constants. However, consider the case where you define a type and want to provide some pre-fab instances of it. E.g., you might want to define a Color class and provide "constants" for common colors like Black, White, etc. It isn\'t possible to do this with const members, as the right hand sides are not compile-time constants. One could do this with regular static members:</p>\n\n<pre><code>public class Color\n{\n    public static Color Black = new Color(0, 0, 0);\n    public static Color White = new Color(255, 255, 255);\n    public static Color Red = new Color(255, 0, 0);\n    public static Color Green = new Color(0, 255, 0);\n    public static Color Blue = new Color(0, 0, 255);\n    private byte red, green, blue;\n\n    public Color(byte r, byte g, byte b) {\n        red = r;\n        green = g;\n        blue = b;\n    }\n}\n</code></pre>\n\n<p>but then there is nothing to keep a client of Color from mucking with it, perhaps by swapping the Black and White values. Needless to say, this would cause consternation for other clients of the Color class. The "readonly" feature addresses this scenario. By simply introducing the readonly keyword in the declarations, we preserve the flexible initialization while preventing client code from mucking around.</p>\n\n<pre><code>public class Color\n{\n    public static readonly Color Black = new Color(0, 0, 0);\n    public static readonly Color White = new Color(255, 255, 255);\n    public static readonly Color Red = new Color(255, 0, 0);\n    public static readonly Color Green = new Color(0, 255, 0);\n    public static readonly Color Blue = new Color(0, 0, 255);\n    private byte red, green, blue;\n\n    public Color(byte r, byte g, byte b) {\n        red = r;\n        green = g;\n        blue = b;\n    }\n}\n</code></pre>\n\n<p>It is interesting to note that const members are always static, whereas a readonly member can be either static or not, just like a regular field.</p>\n\n<p>It is possible to use a single keyword for these two purposes, but this leads to either versioning problems or performance problems. Assume for a moment that we used a single keyword for this (const) and a developer wrote:</p>\n\n<pre><code>public class A\n{\n    public static const C = 0;\n}\n</code></pre>\n\n<p>and a different developer wrote code that relied on A:</p>\n\n<pre><code>public class B\n{\n    static void Main() {\n        Console.WriteLine(A.C);\n    }\n}\n</code></pre>\n\n<p>Now, can the code that is generated rely on the fact that A.C is a compile-time constant? I.e., can the use of A.C simply be replaced by the value 0? If you say "yes" to this, then that means that the developer of A cannot change the way that A.C is initialized -- this ties the hands of the developer of A without permission. If you say "no" to this question then an important optimization is missed. Perhaps the author of A is positive that A.C will always be zero. The use of both const and readonly allows the developer of A to specify the intent. This makes for better versioning behavior and also better performance.</p>\n'}, {'answer_id': 21326956, 'author': 'donstack', 'author_id': 1750877, 'author_profile': 'https://Stackoverflow.com/users/1750877', 'pm_score': 2, 'selected': False, 'text': '<p>ReadOnly :The value will be initialized only once from the constructor of the class.<br>\nconst: can be initialized in any function but only once</p>\n'}, {'answer_id': 22882273, 'author': 'Yonatan Nir', 'author_id': 1164004, 'author_profile': 'https://Stackoverflow.com/users/1164004', 'pm_score': 2, 'selected': False, 'text': '<p>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.  </p>\n\n<p>Remember:\nFor 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.</p>\n\n<p>For details, please refer to C# Frequently Asked Questions on this topic:\n<a href="http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274791.aspx" rel="nofollow">http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274791.aspx</a></p>\n'}, {'answer_id': 27484700, 'author': 'Chirag', 'author_id': 2004144, 'author_profile': 'https://Stackoverflow.com/users/2004144', 'pm_score': 3, 'selected': False, 'text': '<p>There is notable difference between const and readonly fields in C#.Net</p>\n\n<p>const is by default static and needs to be initialized with constant value, which can not be modified later on. Change of value is not allowed in constructors, too. It can not be used with all datatypes. For ex- DateTime. It can not be used with DateTime datatype.</p>\n\n<pre><code>public const DateTime dt = DateTime.Today;  //throws compilation error\npublic const string Name = string.Empty;    //throws compilation error\npublic readonly string Name = string.Empty; //No error, legal\n</code></pre>\n\n<p>readonly can be declared as static, but not necessary. No need to initialize at the time of declaration. Its value can be assigned or changed using constructor. So, it gives advantage when used as instance class member. Two different instantiation may have different value of readonly field. For ex -</p>\n\n<pre><code>class A\n{\n    public readonly int Id;\n\n    public A(int i)\n    {\n        Id = i;\n    }\n}\n</code></pre>\n\n<p>Then readonly field can be initialised with instant specific values, as follows:</p>\n\n<pre><code>A objOne = new A(5);\nA objTwo = new A(10);\n</code></pre>\n\n<p>Here, instance objOne will have value of readonly field as 5 and objTwo has 10. Which is not possible using const.</p>\n'}, {'answer_id': 34750395, 'author': 'Yeasin Abedin', 'author_id': 2672014, 'author_profile': 'https://Stackoverflow.com/users/2672014', 'pm_score': 4, 'selected': False, 'text': '<p><strong>Read Only</strong> :\nValue can be changed through Ctor at runtime. But not through member Function</p>\n<p><strong>Constant</strong> :\nBy default static. Value cannot be changed from anywhere ( Ctor, Function, runtime etc no-where)</p>\n'}, {'answer_id': 52232734, 'author': 'Muhammad VP', 'author_id': 2779217, 'author_profile': 'https://Stackoverflow.com/users/2779217', 'pm_score': 3, 'selected': False, 'text': '<h2>CONST</h2>\n\n<ol>\n<li>const keyword can be applied to fields or local variables</li>\n<li>We must assign const field at time of declaration </li>\n<li>No Memory Allocated Because const value is embedded in IL code itself after compilation.\nIt is like find all occurrences of const variable and replace by its value.\nSo the IL code after compilation will have hard-coded values in place of const variables</li>\n<li>Const in C# are by default static. </li>\n<li>The value is constant for all objects</li>\n<li>There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built  </li>\n<li>Only C# built-in types can be declared as constant</li>\n<li>Const field can not be passed as ref or out parameter</li>\n</ol>\n\n<h2>ReadOnly</h2>\n\n<ol>\n<li>readonly keyword applies only to fields not local variables</li>\n<li>We can assign readonly field at the time of declaration or in constructor,not in any other methods.</li>\n<li>dynamic memory allocated for readonly fields and we can get the value at run time.</li>\n<li>Readonly belongs to the object created so accessed through only instance of class. To make it class member we need to add static keyword before readonly.</li>\n<li>The value may be different depending upon constructor used (as it belongs to object of the class)</li>\n<li>If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.</li>\n<li>Since the value is obtained at run time, there is no dll versioning problem with readonly fields/ properties.</li>\n<li>We can pass readonly field as ref or out parameters in the constructor context.</li>\n</ol>\n'}, {'answer_id': 55910384, 'author': 'Bigeyes', 'author_id': 6476538, 'author_profile': 'https://Stackoverflow.com/users/6476538', 'pm_score': 0, 'selected': False, 'text': '<p><strong>Const</strong>: Absolute constant value during the application life time.</p>\n\n<p><strong>Readonly</strong>: It can be changed in running time. </p>\n'}, {'answer_id': 56450673, 'author': 'Ryan Efendy', 'author_id': 5265992, 'author_profile': 'https://Stackoverflow.com/users/5265992', 'pm_score': 3, 'selected': False, 'text': "<ul>\n<li><p>when to use <code>const</code> or <code>readonly</code></p>\n\n<ul>\n<li><p><code>const</code></p>\n\n<ul>\n<li><strong>compile-time</strong> constant: <strong>absolute</strong> constant, value is set during declaration, is in the IL code itself</li>\n</ul></li>\n<li><p><code>readonly</code></p>\n\n<ul>\n<li><strong>run-time</strong> constant: can be set in the constructor/init via config file i.e. <code>App.config</code>, but once it initializes it can't be changed</li>\n</ul></li>\n</ul></li>\n</ul>\n"}, {'answer_id': 67244864, 'author': 'Moni', 'author_id': 12546097, 'author_profile': 'https://Stackoverflow.com/users/12546097', 'pm_score': 0, 'selected': False, 'text': '<p>The value of readonly field can be changed. However, The value of the const field can not be changed.</p>\n<p>In readonly fields, we can assign values at the time of declaration or in the contructor of that class.Incase of constant we can only assign values at the time of declaration.</p>\n<p>Readonly can be used with Static modifiers but constant cannot be used with static.</p>\n'}]
['https://Stackoverflow.com/questions/55984', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']