C# string.Empty vs double quotes(string.Empty and "" efficiency comparison)

Lionsure 2020-08-31 Original by the website

Different programming methods have very different program efficiency. In order to meet different development needs, each language provides several implementation methods for achieving the same function. Which implementation method should be selected should be determined according to the actual situation, taking into account the efficiency and the difficulty of implementation.

In order for everyone to have a clearer understanding of different implementations, this article first introduces the similarities and differences between string.Empty and "".
 

C# string.Empty vs double quotes(string.Empty and "" efficiency comparison)

I. Similarities

1. Allocate memory

Now, some articles on the Internet say that string.Empty does not allocate memory, but "" allocates an empty storage space. This is a wrong view.

Because whether it is string.Empty or "", storage space is allocated; because the "string" type is a reference type, the allocation method is the same as that of the reference type, that is, the data is stored on the heap, and the address of the object on the heap is stored on the stack on.

 
Specifically, string.Empty and "" will allocate a byte of 4 storage space on the stack, and store the address of the 0-length space stored on the heap. This space stores the data value of string.Empty or "". If you are interested, you can use VS to track memory addresses, which may be clearer.

 

2. CLR optimization

CLR will automatically optimize strings, string.Empty and "" are no exception, they will all be optimized. How is CLR optimized?

 

 Examples are as follows:

1) Optimization of string.Empty

Declare two variables as follows:

string str1 = string.Empty;
       string str2 = string.Empty;

The system will first allocate storage space for "str1" on the heap and stack. When the execution reaches the allocation of storage space for "str2", it finds that there is already similar storage space, so it will not allocate new storage space for "str2", but let "str2 and "str1" use the same storage space to save memory.

 

2) Optimization of ""

The optimization of "" is basically the same as the optimization of string.Empty, so I won't go into details again. The difference is that the optimization method is different, which is introduced in the following differences.

 

II. Difference

The main difference lies in the different optimization methods. First, we must be clear about the internal implementation of string.Empty, as follows:

public static readonly string empty = "";

As can be seen from the code, string.Empty is a static read-only of "", which is essentially "". But because string.Empty is a static type, the optimization method is different.

"" is optimized by the CLR. CLR optimization prevents the creation of duplicate strings in the heap by maintaining a string pool, that is, whenever a new string is created, it checks whether the same string already exists in the heap.

The string.Empty is an optimization at the C# syntax level. When the C# compiler compiles the source code to IL(MSIL), the source code is optimized, that is, all accesses to the static field "Empty" of the "string" class will be directed to the same reference to save memory space.