Should "" be assigned to a string to make it empty?
Or should it be left as null?
You can use any of them, but the canonical way is to assign it a value
string.Empty
IMHO, the best approach is to assign it as String.Empty if you want it to be an empty string (and not null).
. null, . , , , , , , . , - .
public string Foo( string bar ) { bar = bar ?? string.Empty; return bar.ToLower(); }
, , , .
public string Foo( string bar ) { return bar == null ? null : bar.ToLower(); }
Depends on what you are going to do with the string. If you set it to ", this is a valid object and will not throw a null reference exception when you pass it to any function that expects a string. But you also lose the ability to do (fast)" if (str == null) " , and the runtime calls a function call to compare strings.
Source: https://habr.com/ru/post/1747473/More articles:how to enable database logging in an ASP.net application - .netHiding the internal interface in the friend assembly - c #why my C # client that uses library A should have a using statement for library B (which is used by A) - c #border: 2px solid red border VS: 2px red solid - cssread past tapes - cocoaHow to load several mscorlib.dll and call their internal functions separately? - c ++Deeply nested subqueries for moving trees in MySQL - sqlAndroid Autolink launches WebView - androidGrails GSP tag to submit a form with a link? - grailsHow can I manage command line arguments / variables for a script written in Perl? - perlAll Articles