Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does x?.y?.z mean?

The draft spec for Pattern Matching in C# contains the following code example:

Type? v = x?.y?.z; 
if (v.HasValue) {
    var value = v.GetValueOrDefault();     
    // code using value 
} 

I understand that Type? indicates that Type is nullable, but assuming x, y, and z are locals, what does x?.y?.z mean?

like image 856
tkocmathla Avatar asked Aug 07 '14 18:08

tkocmathla


People also ask

What does XYZ mean?

(ĕks′wī-zē′) interj. Informal. Used to indicate to someone that the zipper of his or her pants is open. [ex(amine) y(our) z(ipper).]

What does XYZ mean in coordinates?

Three-dimensional Cartesian coordinate axes. A representation of the three axes of the three-dimensional Cartesian coordinate system. The positive x-axis, positive y-axis, and positive z-axis are the sides labeled by x, y and z. The origin is the intersection of all the axes.

What is XY and Z called?

There are no standard names for the coordinates in the three axes (however, the terms abscissa, ordinate and applicate are sometimes used). The coordinates are often denoted by the letters X, Y, and Z, or x, y, and z. The axes may then be referred to as the X-axis, Y-axis, and Z-axis, respectively.

What is XYZ plane?

The xy-plane is the plane that contains the x- and y-axes; the yz-plane contains the y- and z-axes; the xz-plane contains the x- and z-axes. These three coordinate planes divide space into eight parts, called octants. The first octant, in the foreground, is determined by the positive axes.


3 Answers

Be aware that this language feature is only available in C# 6 and later.

It's effectively the equivalent of:

x == null ? null
   : x.y == null ? null
   : x.y.z

In other words, it's a "safe" way to do x.y.z, where any of the properties along the way might be null.

Also related is the null coalescing operator (??), which provides values to substitute for null.

like image 95
StriplingWarrior Avatar answered Oct 29 '22 01:10

StriplingWarrior


It is Null-propagating operator / Null-Conditional Operator ?. a new proposed feature in C# 6.0

x?.y?.z means

  • first, check if x is not null, then check y otherwise return null,
  • second, when x is not null then check y, if it is not null then return z otherwise return null.

The ultimate return value will be z or null.

Without this operator if x is null, then accessing x.y would raise a Null Reference Exception, the Null-Conditional operator helps to avoid explicitly checking for null.

It is a way to avoid Null Reference Exception.

See: Getting a sense of the upcoming language features in C#

8 - Null-conditional operators

Sometimes code tends to drown a bit in null-checking. The null-conditional operator lets you access members and elements only when the receiver is not-null, providing a null result otherwise:

int? length = customers?.Length; // null if customers is null
like image 30
Habib Avatar answered Oct 29 '22 00:10

Habib


 this.SlimShadies.SingleOrDefault(s => s.IsTheReal)?.PleaseStandUp();

Basically.

like image 3
Phil Avatar answered Oct 28 '22 23:10

Phil