?.
( null coalescing operator ) helps to write clean, simple and clear code. Today I will show 4 situations in which he can be very useful.null
each value in the chain: var location = default(string); if (vendor != null) { if (vendor.ContactPerson != null) { if (vendor.ContactPerson.HomeAddress != null) { location = vendor.ContactPerson.HomeAddress.LineOne; } } }
var location = vendor?.ContactPerson?.HomeAddress?.LineOne;
?.
makes it so that as soon as one of the properties of the chain is null
, no further evaluation of the expression will be performed. Let's see some more examples. INotifyPropertyChanged
interface. In them you can find the following properties: public string Name { get { return name; } set { if (name != value) { name = value; PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } } private string name;
INotifyPropertyChanged.PropertyChanged
event INotifyPropertyChanged.PropertyChanged
no subscribers. In this situation, many developers begin to write like this: public string Name { get { return name; } set { if (name != value) { name = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } } private string name;
PropertyChanged != null
, but before PropertyChanged
called, the program will still crash. And it will happen at the most inopportune moment in a few months after installing the application on the client. Let's fix this annoying problem: save the reference to PropertyChnaged
in a local handler
variable, check it for null
, and then work with this local variable, and not with the PropertyChnaged
public event: public string Name { get { return name; } set { if (name != value) { name = value; var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs("Name")); } } } private string name;
PropertyChanged
correctly every time. In a large program, someone somewhere will surely forget to do all the checks. What to do? C # 6 will save us! All our checks are easy to replace with the operator ?.
and calling the invoke method: public string Name { get { return name; } set { if (name != value) { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); } } } private string name;
IDisposable
. Imagine the evil genius who decided to retire one of his minions. The corresponding code will look like this: public void RetireHenchman() { var disposableMinion = Minion as IDisposable; if (disposableMinion != null) disposableMinion.Dispose(); Minion = null; }
?.
again will help improve the quality of our code: public void RetireHenchman() { (Minion as IDisposable)?.Dispose(); Minion = null; }
?.
may be useful in LINQ queries. Case one: I want to create a query using the SingleOrDefault()
method, and the resulting object (if it exists) access some property. There is nothing easier: var created = members.SingleOrDefault(e => e.name == "dateCreated")?.content;
null
if the source sequence is null
: members?.Select(m => (XElement)XmlValue.MakeValue(m))
null
checks are found and how shorter and more readable the code can be made using the new syntax. Thanks ?.
I began to write code differently. I can’t wait for the final release of C # 6 to be released so that I can give my customers a new version of the source code.Source: https://habr.com/ru/post/252099/
All Articles