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?
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 ...
The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.
+= 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. -=
&& 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.
?.
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With