As far as I know there is not a significantly more elegant way to write the following....
string src; if((ParentContent!= null) &&(ParentContent.Image("thumbnail") != null) &&(ParentContent.Image("thumbnail").Property("src") != null)) src = ParentContent.Image("thumbnail").Property("src").Value
Do you think there should be a C# language feature to make this shorter?
And if so, what should it look like?
for example, something like extending the ?? operator
string src = ParentContent??.Image("thumbnail")??.Property("src")??.Value;
Apologies for the rather contrived example, and my over-simplified solution.
Edit ... Many years later
This is now a planned language feature called the "Null propagating operator" ?.
https://roslyn.codeplex.com/discussions/540883 ( Thanks @Brian )
When calling Any() on a null object, it throws an ArgumentNullException in C#. If the object is null, there definitely aren't 'any', and it should probably return false.
In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.
C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. C# is designed to work with Microsoft's . NET platform.
There is no built-in syntax for doing this, but you can define an extension method to do this:
R NotNull<T, R>(this T src, Func<T, R> f) where T : class where R : class { return src != null ? f(src) : null; }
Now, you can rewrite your example as follows:
src = ParentContent.NotNull(p => p.Image("thumbnail")). NotNull(i => i.Property("src")).NotNull(src => src.Value);
It is not as nice as it may be with a syntactic support, but I'd say it's much more readable.
Note that this adds the NotNull
method to all .NET types, which may be a bit inconvenient. You could solve that by defining a simple wrapper type WrapNull<T> where T : class
containing only a value of type T
and a method for turning any reference type into WrapNull
and providing the NotNull
in the WrapNull
type. Then the code would look like this:
src = WrapNull.Wrap(ParentContent).NotNull(p => p.Image("thumbnail")). NotNull(i => i.Property("src")).NotNull(src => src.Value);
(So you wouldn't pollute the IntelliSense of every type with the new extension method)
With a bit more effort, you could also define a LINQ query operators for doing this. This is a bit overkill, but it is possible to write this (I won't include the definitions here as they are a bit longer, but it's possible in case someone is interested :-)).
src = from p in WrapNull.Wrap(ParentContent) from i in p.Image("thumbnail"). from src in i.Property("src") select src.Value;
It's been suggested and apparently rejected by the team:
A bit more C# syntactic sugar for nulls
The proposed syntax would have looked like a.?b.?c()
- very useful, and unambiguous.
I'd really like to see it too, but doesn't look like it'll happen. Maybe if enough people vote on it!
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