Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 4: how to in-line detect for nulls?

Tags:

null

c#-4.0

In C# 4, wasn't there a short cut for checking for null values like so:

if( myobject?.myproperty?.myotherproperty?.value != null )

The value would return null and not throw an exception.

Anyone have a link to how to use it or at least the syntax?

like image 926
Zachary Scott Avatar asked Jun 07 '10 21:06

Zachary Scott


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 language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.


3 Answers

This operator is called the safe navigation operator in Groovy.

It is not available in C# yet, not even in C# 4.

If enough people show their support for it, maybe it will get into a hypothetical future version of C#...

like image 167
Mark Byers Avatar answered Sep 23 '22 21:09

Mark Byers


Nope, sorry, no such thing. They did consider it, but it didn't make the cut.

like image 24
Joren Avatar answered Sep 22 '22 21:09

Joren


In C# there's also ?? operator that is used for testing against null. This is slightly better than ? operator.

(x ?? -1) is equivalent to (x!= null ? x: -1)

like image 32
kubal5003 Avatar answered Sep 22 '22 21:09

kubal5003