Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the CLR re-use empty strings, but not empty arrays?

Tags:

.net

clr

I notice that

Console.WriteLine((object) new string(' ', 0) == (object) new string(' ', 0));

prints true, which indicates that the CLR keeps the empty string around and re-uses the same instance. (It prints false for any other number than 0.)

However, the same is not true for arrays:

Console.WriteLine(new int[0] == new int[0]);   // False

Now, if we look at the implementation of Enumerable.Empty<T>(), we find that it caches and re-uses empty arrays:

public static IEnumerable<TResult> Empty<TResult>()
{
    return EmptyEnumerable<TResult>.Instance;
}

[...]

public static IEnumerable<TElement> Instance
{
    get
    {
        if (EmptyEnumerable<TElement>.instance == null)
            EmptyEnumerable<TElement>.instance = new TElement[0];
        return EmptyEnumerable<TElement>.instance;
    }
}

So the framework team felt that keeping an empty array around for every type is worth it. The CLR could, if it wanted to, go a small step further and do this natively so it applies not only to calls to Enumerable.Empty<T>() but also new T[0]. If the optimisation in Enumerable.Empty<T>() is worth it, surely this would be even more worth it?

Why does the CLR not do this? Is there something I’m missing?

like image 984
Timwi Avatar asked Oct 27 '11 12:10

Timwi


People also ask

Why do we use empty strings?

A string though is immutable, so any attempt to change the string (either inside or outside a function) is not possible (in fact Python has no features to change the content of string). This means that it behaves as if the string is passed by value - changes to it are not reflected back to the caller.

How do you fill an array with empty strings?

To create an array of empty strings with JavaScript, we can use the Array function and the fill method. to create an array with 20 empty slots with Array(20) . Then we call fill with an empty string to fill the empty slots with empty strings. Therefore, arr is an array with 20 empty strings.

Does the empty language contain the empty string?

In formal treatments, the empty string is denoted with ε or sometimes Λ or λ. The empty string should not be confused with the empty language ∅, which is a formal language (i.e. a set of strings) that contains no strings, not even the empty string. The empty string has several properties: |ε| = 0.

Is string empty actually better than in C #?

"" is always a new object, which may have to be removed by the garbage collector. Therefore you should always use string. empty instead of "". The compiler will "link" them all to an empty string.

What is the use of string empty in C#?

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

Strings may use interning, that makes them a different story (from all other kind of objects).

Arrays are essentially just objects. Re-using instances where that is not clear from the syntax or context isn't without side effects or risks.

static int[] empty = new int[0];
...
   lock (empty) { ... }

If some other code locked on another (they thought) empty int[] you might have a deadlock that is very hard to find.

Other scenarios include using arrays as the key in a Dictionary, or anywhere else their identity matters. The framework can't just go around changing the rules.

like image 109
Henk Holterman Avatar answered Sep 28 '22 06:09

Henk Holterman