Is there a way to implement and make use of a "NOT null coalescing" operator?

Saro Taşciyan

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.

Marc Gravell

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
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a way to implement and make use of a "NOT null coalescing" operator?

From Java

Is there a "null coalescing" operator in JavaScript?

From Dev

Null coalescing operator override

From Dev

Null coalescing operator with bool

From Dev

Null coalescing operator and casting with as

From Dev

Null coalescing operator override

From Java

PHP Null coalescing operator usage

From Dev

Null Coalescing Operator in F#?

From Dev

null coalescing operator assignment to self

From Dev

null-coalescing operator in the getter

From Java

PHP ternary operator vs null coalescing operator

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

null-coalescing operator on a property get

From Dev

Implicit conversion with null-coalescing operator

From Java

What is null coalescing assignment ??= operator in PHP 7.4

From Dev

Is there an equal for the c# null coalescing operator in java?

From Dev

null-coalescing operator with wildcard in a lambda expression

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

sql null in c# and null coalescing operator

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

How to define a null-coalescing operator for Twig?

From Dev

Performance of expression behind null coalescing operator

From Java

Curious null-coalescing operator custom implicit conversion behaviour

From Dev

How does the Null-Coalescing Operator (??) work in Spider?

From Dev

Shorthand for null-coalescing operator and assignment for re-generatable property?

From Dev

Null-coalescing operator returning null for properties of dynamic objects

From Java

Null coalescing operator IList, Array, Enumerable.Empty in foreach

From Dev

Why doesn't the null coalescing operator (??) work in this situation?

Related Related

  1. 1

    Is there a way to implement and make use of a "NOT null coalescing" operator?

  2. 2

    Is there a "null coalescing" operator in JavaScript?

  3. 3

    Null coalescing operator override

  4. 4

    Null coalescing operator with bool

  5. 5

    Null coalescing operator and casting with as

  6. 6

    Null coalescing operator override

  7. 7

    PHP Null coalescing operator usage

  8. 8

    Null Coalescing Operator in F#?

  9. 9

    null coalescing operator assignment to self

  10. 10

    null-coalescing operator in the getter

  11. 11

    PHP ternary operator vs null coalescing operator

  12. 12

    Usage of ?? operator (null-coalescing operator)

  13. 13

    Usage of ?? operator (null-coalescing operator)

  14. 14

    null-coalescing operator on a property get

  15. 15

    Implicit conversion with null-coalescing operator

  16. 16

    What is null coalescing assignment ??= operator in PHP 7.4

  17. 17

    Is there an equal for the c# null coalescing operator in java?

  18. 18

    null-coalescing operator with wildcard in a lambda expression

  19. 19

    Is there an equivalent of isset or a null coalescing operator in SASS?

  20. 20

    sql null in c# and null coalescing operator

  21. 21

    Is there an equivalent of isset or a null coalescing operator in SASS?

  22. 22

    How to define a null-coalescing operator for Twig?

  23. 23

    Performance of expression behind null coalescing operator

  24. 24

    Curious null-coalescing operator custom implicit conversion behaviour

  25. 25

    How does the Null-Coalescing Operator (??) work in Spider?

  26. 26

    Shorthand for null-coalescing operator and assignment for re-generatable property?

  27. 27

    Null-coalescing operator returning null for properties of dynamic objects

  28. 28

    Null coalescing operator IList, Array, Enumerable.Empty in foreach

  29. 29

    Why doesn't the null coalescing operator (??) work in this situation?

HotTag

Archive