Phoque
Phoque

Reputation: 217

MSDN Tuple equality C#7.3 weird line

I was reading about the ValueTuple on MSDN and there is one line of code I don't understand in this sample presenting C#7.3's new tuple equality :

var left = (a: 5, b: 10);
var right = (a: 5, b: 10);
(int a, int b)? nullableTuple = right; //this line here
Console.WriteLine(left == nullableTuple);

I'm used to '?' syntax like a==b?c():d(); or c?.ToString(); to test conditions or nullable values but this one I don't quite understand how it works.

Upvotes: 0

Views: 88

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156978

(int a, int b)? is just a tuple (int a, int b) which is nullable ((int a, int b)? or actually Nullable<(int a, int b)>).

Since structs (and so tuples) can't be null, you have to wrap them in a nullable to be able to make them be nullable. That is what the question mark does.

You might be used to similar cases with int, which can't be null. int? can.

Upvotes: 4

Related Questions