Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of confusing comment above "string.Empty" in .NET/BCL source?

I'm trying to understand why string.Empty is readonly and not a const. I saw this Post but I don't understand the comment Microsoft wrote about it. As Jon Skeet wrote in a comment "I don't know - it doesn't make much sense to me, to be honest..."

Shared Source Common Language Infrastructure 2.0 Release. string.cs is in sscli20\clr\src\bcl\system\string.cs

// The Empty constant holds the empty string value. //We need to call the String constructor so that the compiler doesn't mark this as a literal. //Marking this as a literal would mean that it doesn't show up as a field which we can access  //from native. public static readonly String Empty = "";  

I can't see here any String constructor call and furthermore, it's marked as literal - ""

Can someone please explain me in plain text, What does the comment mean and why is string.Empty readonly and not a const?


Update:
Eric Lippert commented on by now a deleted answer:

I asked one of the C# old-timers over lunch about this and he did not recall specifically why this decision was made, but conjectured that it had something to do with interning.

like image 265
gdoron is supporting Monica Avatar asked Dec 13 '11 07:12

gdoron is supporting Monica


People also ask

What is the meaning of string empty?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null . It can be described as the absence of a string instance.

Why is string empty not const?

Originally: Because String is a class and therefore cannot be a constant.

What is C# string empty?

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String. Empty (A constant for empty strings).


1 Answers

The important part is not what happens IN this class, but what happens, when another class uses (and links to) it. Let me explain with another example:

Assume you have a Assembly1.dll containing a class declaring

public static const int SOME_ERROR_CODE=0x10; public static readonly int SOME_OTHER_ERROR_CODE=0x20; 

and another class consuming this e.g.

public int TryFoo() {     try {foo();}     catch (InvalidParameterException) {return SOME_ERROR_CODE;}     catch (Exception) { return SOME_OTHER_ERROR_CODE;}     return 0x00; } 

You compile your class into Assembly2.dll and link it against Assembly1.dll, as expected, your method will return 0x10 on invalid parameters, 0x20 on other errors, 0x00 on success.

Especially, if you create Assembly3.exe containing something like

int errorcode=TryFoo(); if (errorcode==SOME_ERROR_CODE) bar(); else if (errorcode==SOME_OTHER_ERROR_CODE) baz(); 

It will work as expected (After being linked against Assembly1.dll and Assembly2.dll)

Now if you get a new version of Assembly1.dll, that has

public const int SOME_ERROR_CODE=0x11; public readonly int SOME_OTHER_ERROR_CODE=0x21; 

If you recompile Assembly3.exe and link the last fragment against new Assembly1.dll and unchanged Assembly2.dll, it will stop working as expected:

bar() will NOT be called correctly: Assembly2.dll remembers the LITERAL 0x20, which is not the same literal 0x21 that Assembly3.exe reads out of Assembly1.dll

baz() will be called correctly: Both Assembly2.dll and Assembly3.exe refer to the SYMBOL REFERENCE called SOME_OTHER_ERROR_CODE, which is in both cases resolved by the current version of Assembly1.dll, thus in both cases is 0x21.

In Short: a const creates a LITERAL, a readonly creates a SYMBOL REFERENCE.

LITERALS are internal to the framework and can not be marshalled and thus used by native code.

So

public static readonly String Empty = "";  

creates a symbol reference (resovled at time of first use by a call to the String cosntuctor), that can be marshalled an thus used from native, while

public static const String Empty = "";  

would create a literal, that can't.

like image 99
Eugen Rieck Avatar answered Sep 29 '22 09:09

Eugen Rieck