5

Is there a not null coalescing operator in C# which in case could be used such as:

public void Foo(string arg1)
{
    Bar b = arg1 !?? Bar.Parse(arg1);   
}

The following case made me think of it:

public void SomeMethod(string strStartDate)
{
    DateTime? dtStartDate = strStartDate !?? DateTime.ParseExact(strStartDate, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}

I might not have strStartDate information, which in case will be null but if i do; i'm always certain that it will be in expected format. So instead of initializing dtStartDate = null and trying to parse and set the value within try catch block. It seems to be more useful.

I suppose the answer is no (and there is no such operator !?? or anything else) I wonder if there's a way to implement this logic, would it be worth and what would be the cases that it comes useful.

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
  • 1
    why don't you use just [conditional ?: Operator](http://msdn.microsoft.com/en-us/library/ty67wk28.aspx) ? – Selman Genç Jan 29 '14 at 08:13
  • what do you mean by "not null coalescing"? If you wrote what it did in regular "this compiles now" code (using either `if` or *conditional*), what would it look like? – Marc Gravell Jan 29 '14 at 08:17
  • @Selman22 Isn't *null coalescing* a syntactic sugar for *tenary*? You could `x = x != null ? x : y` instead of `x = x ?? y` aswell. I suppose this is not the case, sorry kardeşim – Saro Taşciyan Jan 29 '14 at 08:17
  • 1
    @Zefnus you can **NOT** add a new operator to an existing language.If you want it design and create your own language, kardeşim. – Selman Genç Jan 29 '14 at 08:18
  • 2
    @Zefnus no, the `??` operator is syntactic sugar for "take the first non-null expression"; it is also possible to *represent* that in a *condtional*, but that doesn't mean that `??` is syntactic sugar for a *conditional* – Marc Gravell Jan 29 '14 at 08:19
  • @MarcGravell well i suppose the syntactic sugar i was looking for was "take the first null expression" in case. And if none it would take a value – Saro Taşciyan Jan 29 '14 at 08:21
  • 1
    The "take the first null expression" is under consideration (at least, as far as member access) for C# 6 - see my answer – Marc Gravell Jan 29 '14 at 08:27
  • 1
    Some other questions with a similar topic: [Possible pitfalls of using this (extension method based) shorthand](http://stackoverflow.com/questions/123088/possible-pitfalls-of-using-this-extension-method-based-shorthand), [Evil use of Maybe monad and extension methods in C#?](http://stackoverflow.com/questions/1196031/evil-use-of-maybe-monad-and-extension-methods-in-c), [How to check for nulls in a deep lambda expression?](http://stackoverflow.com/questions/854591/how-to-check-for-nulls-in-a-deep-lambda-expression). – poke Jan 29 '14 at 08:35

2 Answers2

6

Mads Torgersen has publicly said that a null-propagating operator is under consideration for the next version of C# (but also emphasised that this doesn't mean it will be there). This would allow code like:

var value = someValue?.Method()?.AnotherMethod();

where the ?. returns null if the operand (on the left) is null, else will evaluate the right hand side. I suspect that would get you a lot of the way here, especially if combined with (say) extension methods; for example:

DateTime? dtStartDate = strStartDate?.MyParse();

where:

static DateTime MyParse(this string value) {
    return DateTime.ParseExact(value, "dd.MM.yyyy",
         System.Globalization.CultureInfo.InvariantCulture
);

However! You could do the same thing right now just using extension methods:

DateTime? dtStartDate = strStartDate.MyParse();

static DateTime? MyParse(this string value) {
    if(value == null) return null;
    return DateTime.ParseExact(value, "dd.MM.yyyy",
         System.Globalization.CultureInfo.InvariantCulture
);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • That is the answer, thank you. I considered your *right now* suggestion as alternative for using conditional operator – Saro Taşciyan Jan 29 '14 at 08:32
  • Do you know if any consideration was given to, instead of only having an operator that maps null to null, having `foo ?.bar : baz` be equivalent [but for `foo` being evaluated only once] to `foo ? foo.bar : baz`? That would I think both make the syntax more useful and also bring it closer to `? :`. – supercat Apr 03 '14 at 19:31
2

Just use the ternary conditional operator ?::

DateTime? dtStartDate = strStartDate == null ? null : DateTime.ParseExact(…)

An operator you proposed isn’t actually easily doable because it has a non-consistent type:

DateTime? a = (string)b !?? (DateTime)c;

For this expression to work, the compiler would need to know at compile time that b is null, so that the (null) string value can be assigned to a.

poke
  • 369,085
  • 72
  • 557
  • 602