What is the best practice for using in strings in ASP.NET (C #)

I want to know what is the preferred way to declare a string in C #:

string strEmpty = String.Empty;

  or

string strNull = null;

Which practice is better or what is the difference between these statements.

+3
source share
5 answers

the first answer makes the string value the actual empty string. Setting it to null indicates that the pointer has nothing to do. This means that if you try to execute strEmpty.Function (), in the second case this will not work.

At first, more memory is taken, but more clearly.

, . , null. ( , ..), string.empty.

+7

.

, NULL, , . , , .

, . , , , , .

, , URL-, . , , , . NULL, - , .

, - URL- NULL.

+7

"". , , , .

0, . , - , . .

- , . , , .

+3

- : Null , , Empty , .

In other types of applications, the semantics are more or less up to you if you document them and apply them consistently in your team.

There is no difference in performance.

+2
source

The difference between the null and empty line is really semantic, but they are basically equivalent by convention.

In .NET, a class System.Stringhas a static method IsNullOrEmptythat is best used in most cases.

+1
source

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


All Articles