Timeline for What is the difference between const and readonly in C#?
Current License: CC BY-SA 4.0
27 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 3 at 14:58 | comment | added | B H | By using 'public static' instead of 'public readonly', you get the ability to have a computed value while making that value accessible without an instantiation of the class. I typically use this approach when I want to use an array of values like a constant. Readonly would require class instantiation and compute the value for each instantiation. | |
Jul 11, 2023 at 12:06 | comment | added | krillgar | @CADbloke provided a link that is now broken. Jon reposted that blog post on his own site | |
Nov 23, 2022 at 8:06 | history | edited | fat | CC BY-SA 4.0 |
spelling
|
Oct 30, 2022 at 11:34 | comment | added | Parveen | Here is the detail explanation for const vs readonly youtu.be/jA30qZNGNoM | |
Sep 17, 2021 at 12:46 | comment | added | N4ppeL |
Your suggestions when to use what seems a good pointer, however, for private fields you can always use const , since that will always be recompiled after a change, right?
|
|
Jul 29, 2021 at 5:45 | comment | added | Shahryar Saljoughi |
As the expected answer, It would be great if your answer also include the point that the value being assigned to a const field, should be completely defined at compile time. That said, you can't assign something like new MyClass() to a const , since this way the value is being constructed at runtime.
|
|
Apr 21, 2021 at 19:04 | history | edited | Olivia Stork | CC BY-SA 4.0 |
minor grammar and formatting fixes, for readability
|
S Sep 22, 2015 at 8:01 | history | suggested | Irshad | CC BY-SA 3.0 |
formatted..
|
Sep 22, 2015 at 7:38 | review | Suggested edits | |||
S Sep 22, 2015 at 8:01 | |||||
Aug 18, 2015 at 10:38 | comment | added | dragan.stepanovic | This is the same case as with method's default arguments. As with constants, their default value is embedded in the client's call, and if you change the value of the default argument in the method definition and recompile just that assembly, client assemblies will still have the old value. | |
S Sep 29, 2014 at 6:46 | history | suggested | Serge P | CC BY-SA 3.0 |
made naming more consistent (AssemblyA, AssemblyB)
|
Sep 29, 2014 at 6:17 | review | Suggested edits | |||
S Sep 29, 2014 at 6:46 | |||||
Aug 5, 2014 at 21:52 | comment | added | CAD bloke | blogs.msmvps.com/jonskeet/2014/07/16/… is an interesting read only the overhead cost of readonly | |
May 7, 2014 at 22:37 | history | edited | Noctis | CC BY-SA 3.0 |
english
|
Apr 14, 2014 at 13:37 | comment | added | user743382 |
@mini-me That doesn't contradict what I said. They're not allowed to be changed, the CIL spec explicitly forbids it, but the runtime doesn't enforce it, and the CIL spec doesn't require (but does allow) the runtime to enforce it. Code modifying readonly fields is an active time bomb, just waiting to blow up when the runtime does start enforcing this.
|
|
Apr 14, 2014 at 11:55 | comment | added | user743382 |
@mini-me readonly variables are not allowed to be changed once the constructor has finished, even via reflection. The runtime happens to not enforce this. The runtime also happens not to enforce that you don't change string.Empty to "Hello, world!" , but I still wouldn't claim that this makes string.Empty modifiable, or that code shouldn't assume that string.Empty will always be a zero-length string.
|
|
S Mar 26, 2014 at 21:42 | history | suggested | David says Reinstate Monica | CC BY-SA 3.0 |
fixed spelling
|
Mar 26, 2014 at 21:40 | review | Suggested edits | |||
S Mar 26, 2014 at 21:42 | |||||
Jun 5, 2013 at 14:10 | comment | added | Bitterblue |
readonly variables can be changed outside the constructor (reflection). It's only the compiler that tries to hinder you from modifying the var outside the constructor.
|
|
Jun 5, 2013 at 7:03 | comment | added | CodingBarfield | The part about reference values is the most important one. Const values can be optimized away. | |
Jan 21, 2013 at 14:00 | comment | added | LCJ |
The static point seems to be the most important and useful point - consts are implicitly static
|
|
Aug 31, 2011 at 7:33 | review | Suggested edits | |||
Aug 31, 2011 at 7:36 | |||||
Aug 24, 2009 at 2:59 | vote | accept | readonly | ||
Sep 11, 2008 at 10:13 | history | edited | Gishu | CC BY-SA 2.5 |
Added Book link.
|
Sep 11, 2008 at 8:50 | history | edited | Gishu | CC BY-SA 2.5 |
readonly can be computed.
|
Sep 11, 2008 at 8:30 | history | edited | Gishu | CC BY-SA 2.5 |
improved formatting
|
Sep 11, 2008 at 8:24 | history | answered | Gishu | CC BY-SA 2.5 |