In .Net why is String.Empty read only instead of a constant? I'm just wondering if anyone knows what the reasoning was behind that decision.
The reason that
I found this information from this handy article at CodeProject.
I think there is a lot of confusion and bad responses here.
First of all,
Check section 10.4 Constants of the C# language specification.
Given this, the following lines of code do exactly the same thing in respect to the creation of a new Object.
Here is a note from Microsoft that explains the difference between the 2:
So I find that the only plausible answer here is Jeff Yates's.
If you make any string constant, then the compiler is replace with the actually string everywhere you call it and you fill your code with the same string all over and when the code runs is also need to read again and again that string from the different memory data.
If you leave your string read only on one place as it is the
Also if you compile any dll using the String.Empty as const, and for any reason the String.Empty change, then the compiled dll will not work any more the same, because the
See this code for example:
will be come by the compiler as:
and the assembly call
This answer exists for historical purposes.
A lot of useful dialog was hammered out in vetting this answer, and rather than deleting it, this content is reproduced directly: