VS proposed to "simplify"
... EventHandler<MyEventArgs> args = MyHandler if (args != null) { args(this, e); } ... On
... MyHandler?.Invoke(this, e) ... What does it mean? Where to dig?
args = MyHandler if (args != null) { arg...">
In C # 6, a so-called null propagation operator appeared. It allows you to simplify quite tedious checks on null, and means that if the expression on its left side is null, then it will return null, and if it has some other value, the field / property value from the right side will be returned, or The method from the right side is executed. Agree to write
data = some?.GetData(); clearly more convenient than
if(some != null) data = some.GetData(); This design
MyHandler?.Invoke(this, e); logically equivalent to the following
if ( MyHandler != null ) MyHandler.Invoke(this, e); The same construction was introduced for the index operator. for example
int[] a = null; int? x = a?[0]; a = new int[1] { 10 }; int? b = a?[0]; This tool appeared in the new version of C # 6.0
MyHandler should be evaluated only once, and for you it is evaluated twice when the condition MyHandler != null true. - PetSerAlThe keyword
nullis a literal representing a null reference that does not reference any object.nullis the default value of reference type variables.
For the keyword null , special operators are defined:
int? a;null for the value type . Instead of int you can specify any type of value.obj?.SomeMethod();obj object for null and, if the object is not null , we refer to the type member.?. can I contact the indexer ?[] .int y = x ?? -1;x != null , take its value, otherwise do we execute the construction to the right of ?? , in this case, set y = -1 .You can also combine with each other:
int? length = customers?.Length ?? -1; // если customers != null, вернуть customers.Length, иначе вернуть -1 Source: https://ru.stackoverflow.com/questions/607355/
All Articles