Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This answer exists for historical purposes.</p> <p><strong>Originally:</strong></p> <p>Because <code>String</code> is a class and therefore cannot be a constant.</p> <p><strong>Extended Discussion:</strong></p> <p>A lot of useful dialog was hammered out in vetting this answer, and rather than deleting it, this content is reproduced directly:</p> <blockquote> <p>In .NET, (unlike in Java) string and String are exactly the same. And yes, you can have string literal constants in .NET – DrJokepu Feb 3 '09 at 16:57</p> <p>Are you saying that a Class cannot have constants? – StingyJack Feb 3 '09 at 16:58</p> <p><em>Yes, objects have to use readonly. Only structs can do constants. I think when you use <code>string</code> instead of <code>String</code> the compiler changes the const into a readonly for you. All to do with keeping C programmers happy. – Garry Shutler Feb 3 '09 at 16:59</em></p> <p>tvanfosson just explained it a little bit more verbose. "X cannot be a constant, because the containing Y is a class" was just a little bit too context-free ;) – Leonidas Feb 3 '09 at 17:01</p> <p>string.Empty is static property that returns an instance of the String class, namely the empty string, not the string class itself. – tvanfosson Feb 3 '09 at 17:01</p> <p>Empty is a readonly instance (it's not a property) of the String class. – senfo Feb 3 '09 at 17:02</p> <p><em>Head hurting. I still think I'm right, but now I'm less certain. Research required tonight! – Garry Shutler Feb 3 '09 at 17:07</em></p> <p>The empty string is an instance of the string class. Empty is a static field (not a property, I stand corrected) on the String class. Basically the difference between a pointer and the thing it points to. If it weren't readonly we could change which instance the Empty field refers to. – tvanfosson Feb 3 '09 at 17:07</p> <p>Garry, you don't need to do any research. Think about it. String is a class. Empty is an instance of a String. – senfo Feb 3 '09 at 17:12</p> <p>There is something I don't quite get: how on earth can the static constructor of the String class create an instance of the String class ? Isn't that some sort of "chicken or the egg" scenario? – DrJokepu Feb 3 '09 at 17:12 5 </p> <p>This answer would be correct for nearly any other class but System.String. .NET does a lot of performance special-casing for strings, and one of them is that you CAN have string constants, just try it. In this case, Jeff Yates has the correct answer. – Joel Mueller Feb 3 '09 at 19:25</p> <p>As described in §7.18, a constant-expression is an expression that can be fully evaluated at compile-time. Since the only way to create a non-null value of a reference-type other than string is to apply the new operator, and since the new operator is not permitted in a constant-expression, the only possible value for constants of reference-types other than string is null. The previous two comments were taken directly from the C# language specification and reiterate what Joel Mueller mentioned. – senfo Feb 4 '09 at 15:05 5</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload