Most advisable way of checking empty strings in C#

后端 未结 7 876
抹茶落季
抹茶落季 2020-12-13 01:51

What is the best way for checking empty strings (I\'m not asking about initializing!) in C# when considering code performance?(see code bel

7条回答
  •  猫巷女王i
    2020-12-13 02:28

    Do not compare strings to String.Empty or "" to check for empty strings.

    Instead, compare by using String.Length == 0

    The difference between string.Empty and "" is very small. String.Empty will not create any object while "" will create a new object in the memory for the checking. Hence string.empty is better in memory management. But the comparison with string.Length == 0 will be even faster and the better way to check for the empty string.

提交回复
热议问题