Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In c# resharper suggested "conditional access", what does null give me?

Tags:

c#

I use Resharper to help with language features and I have a DateTime field that is nullable. Resharper suggested this syntax:

 TodayDate = paidDate?.ToString("d"),

It looks like a standard expresson but I don't get one question mark. two question marks I get and colon I get.

An explanation would be appreciated. whathappens when paidDate is null?

like image 647
Peter Kellner Avatar asked Aug 24 '15 15:08

Peter Kellner


People also ask

What is '~' in C language?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What does the || mean in C?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What does += in C mean?

+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A. -=

What is & operator in C?

&& This is the AND operator in C programming language. It performs logical conjunction of two expressions. ( If both expressions evaluate to True, then the result is True.


1 Answers

?. is a new feature introduced in C# and it's called Null-conditional Operators. It evaluates method call only when paidDate is not null, and returns null instead.

It's pretty much equivalent to

TodayDate = paidDate == null ? null : paidDate.ToString("d");

If you try calling a method that returns value type after ?. it will make the whole thing return Nullable<T> of that value type, e.g.

var myValue = paidDate?.Day;

would make myValue be typed as Nullable<int>.

like image 57
MarcinJuraszek Avatar answered Nov 11 '22 15:11

MarcinJuraszek