Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't String.Empty a constant?

People also ask

Why is string empty not a constant?

string. Empty is a static readonly field, not a literal constant. This precludes its use anywhere that requires a compile-time constant, e.g. attribute constructors and default parameters.

Is string empty equal to?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all.

Can a string be a constant?

A String Literal, also known as a string constant or constant string, is a string of characters enclosed in double quotes, such as "To err is human - To really foul things up requires a computer." String literals are stored in C as an array of chars, terminted by a null byte.

How does empty string work?

The empty string is the special case where the sequence has length zero, so there are no symbols in the string. There is only one empty string, because two strings are only different if they have different lengths or a different sequence of symbols.


The reason that static readonly is used instead of const is due to use with unmanaged code, as indicated by Microsoft here in the Shared Source Common Language Infrastructure 2.0 Release. The file to look at is 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.

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, const fields are static members (not instance members).

Check section 10.4 Constants of the C# language specification.

Even though constants are considered static members, a constant-declaration neither requires nor allows a static modifier.

If public const members are static, one could not consider that a constant will create a new Object.

Given this, the following lines of code do exactly the same thing in respect to the creation of a new Object.

public static readonly string Empty = "";
public const string Empty = "";

Here is a note from Microsoft that explains the difference between the 2:

The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants, ...

So I find that the only plausible answer here is Jeff Yates's.


String.Empty read only instead of a constant?

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 String.Empty, the program keep the same string only on one place and read it, or refer to it - keeping the data in memory minimum.

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 cost make the inside code to actually keep a copy of the string on every call.

See this code for example:

public class OneName
{
    const string cConst = "constant string";
    static string cStatic = "static string";
    readonly string cReadOnly = "read only string";

    protected void Fun()
    {
        string cAddThemAll ;

        cAddThemAll = cConst;
        cAddThemAll = cStatic ;
        cAddThemAll = cReadOnly;    
    }
}

will be come by the compiler as:

public class OneName
{
    // note that the const exist also here !
    private const string cConst = "constant string";
    private readonly string cReadOnly;
    private static string cStatic;

    static OneName()
    {
        cStatic = "static string";
    }

    public OneName()
    {
        this.cReadOnly = "read only string";
    }

    protected void Fun()
    {
        string cAddThemAll ;

        // look here, will replace the const string everywhere is finds it.
        cAddThemAll = "constant string";
        cAddThemAll = cStatic;
        // but the read only will only get it from "one place".
        cAddThemAll = this.cReadOnly;

    }
}

and the assembly call

        cAddThemAll = cConst;
0000003e  mov         eax,dword ptr ds:[09379C0Ch] 
00000044  mov         dword ptr [ebp-44h],eax 
        cAddThemAll = cStatic ;
00000047  mov         eax,dword ptr ds:[094E8C44h] 
0000004c  mov         dword ptr [ebp-44h],eax 
        cAddThemAll = cReadOnly;
0000004f  mov         eax,dword ptr [ebp-3Ch] 
00000052  mov         eax,dword ptr [eax+0000017Ch] 
00000058  mov         dword ptr [ebp-44h],eax 

Edit: Corrected typo