@hvd
Null Conditional Operator (?.) Null-Coalescing (??)
string userName = null;
int len = userName.Length;
userName.Length
: " " System.NullReferenceException " ConsoleApplication...
: . "
string userName = null;
int? len = userName.Length;
int? len = userName?.Length;
null
. , int? (Nullable int), , , , 0.
, (?.) (??)
int len = userName?.Length ?? 0;
.
, ,
myCustomEventHandler?.Invoke(, EventArgs.Empty);
where "myCustomEventHandler" is a public event prior to C # 6.0, we must create a local copy of "myCustomEventHandler", otherwise this may throw an exception if muti-threaded is used. whereas in C # 6 we can directly call "myCustomEventHandler" without creating a local copy, and this is thread safe.
eg. in c # 5
var handler= this.myCustomEventHandler;
if (handler!= null)
handler(…)
in c # 6
myCustomEventHandler?.Invoke(e)
source
share