Why string.Empty is more efficient than "" in .Net

I'm sure string.Empty is more efficient than using "" in .NET. What I would like to know, WHY is it more efficient?

+3
source share
7 answers

I think in most cases there is no difference. In normal cases, when you use ""in your code, this string will be interned and the same instance of the string will be reused. Thus, when used ""compared to String.Emptythere will be no more line instances.

If you want to prove:

Dim a As String
Dim b As String

a = ""
b = ""

Console.WriteLine(Object.ReferenceEquals(a, b)) ' Prints True '

True , String.Empty, .

: .

+13

, , . , , - : .

, .NET, String.empty, , . .

( )

object obj = "";
string str1 = "";
string str2 = String.Empty;
Console.WriteLine(obj == str1); // true
Console.WriteLine(str1 == str2); // true
Console.WriteLine(obj == str2); // sometimes true, sometimes false?!
+6

, - string.Empty "".

.

+2

"" . ,.NET , . String.Empty, , .

+2
+1

Pre 2.0, it was correct, which is String.Emptymore efficient, but since then there is no difference. See What is the difference between String.Empty and "" (empty string)?

+1
source

string.Empty is a singleton string constant string constant, but "" will create a new empty string.

0
source

Source: https://habr.com/ru/post/1725391/


All Articles