Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Null propagating operator / Conditional access expression & if blocks

Tags:

The Null propagating operator / Conditional access expression coming in c#-6.0 looks like quite a handy feature. But I'm curious if it will help solve the problem of checking if a child member is not null and then calling a Boolean method on said child member inside an if block:

  public class Container<int>{        IEnumerable<int> Objects {get;set;}   }    public Container BuildContainer()   {        var c = new Container();        if (/* Some Random Condition */)          c.Objects = new List<int>{1,2,4};   }    public void Test()   {       var c = BuildContainer();        //Old way       if ( null != c && null != c.Objects && c.Objects.Any())          Console.Write("Container has items!");         //C# 6 way?       if (c?.Object?.Any())           Console.Write("Container has items!");   } 

Will c?.Object?.Any() compile? If the propagating operator short circuits (I assume that's the right term) to null then you have if (null), which isn't valid.

Will the C# team address this concern or am I missing the intended use case for the null propagating operator?

like image 948
Philip Pittle Avatar asked Sep 04 '14 13:09

Philip Pittle


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

It won't work this way. You can just skip the explanation and see the code below :)

As you know ?. operator will return null if a child member is null. But what happens if we try to get a non-nullable member, like the Any() method, that returns bool? The answer is that the compiler will "wrap" a return value in Nullable<>. For example, Object?.Any() will give us bool? (which is Nullable<bool>), not bool.

The only thing that doesn't let us use this expression in the if statement is that it can't be implicitly casted to bool. But you can do comparison explicitly, I prefer comparing to true like this:

if (c?.Object?.Any() == true)     Console.Write("Container has items!"); 

Thanks to @DaveSexton there's another way:

if (c?.Object?.Any() ?? false)     Console.Write("Container has items!"); 

But as for me, comparison to true seems more natural :)

like image 171
Eldar Dordzhiev Avatar answered Sep 17 '22 12:09

Eldar Dordzhiev