Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does object?.Property mean in c# [duplicate]

Tags:

c#

.net

I recently came across this while going through someone else's code

var name = Product.Buyer?.FirstName + " " + Product.Buyer?.LastName;

What does this(?.)mean in c#

like image 438
Anshuman Jasrotia Avatar asked Dec 08 '22 21:12

Anshuman Jasrotia


1 Answers

The operator ?. is called Null-conditional Operators, which is introduced in C# 6.0.

Used to test for null before performing member access (?.) or index (?[) operation. These operators help you write less code to handle null checks, especially for descending into data structures.

see the documentation and an example here

like image 113
sujith karivelil Avatar answered Dec 22 '22 00:12

sujith karivelil