Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null parameter alternative C# 6.0

Tags:

c#

c#-6.0

I have seen new feature in C# 6 which allows code to skip if statements for null checks.

For example:

return p?.ToString();

How can this be done for calling a method to which p needs to be passed as parameter (without old if/else)?

The way I would normally write this with C# pre-6:

 p != null ? callmethod(p) : null

is there something better in C# 6?

like image 439
Mandar Jogalekar Avatar asked Jul 15 '17 09:07

Mandar Jogalekar


People also ask

What to use instead of null?

String data types are often initalized to null as well. However, for the exact same reasons, we prefer to use an empty string to replace null .

How do I return a generic null?

So, to return a null or default value from a generic method we can make use default(). default(T) will return the default object of the type which is provided.

How do you handle null values in C sharp?

Empty(A constant for empty strings). This method will take a parameter that will be of System. String type. The method will return a Boolean value, like n case if the argument String type has null or empty string (“”), then it will return True value else it will return False value.

What can I use instead of null in Java?

Throwing an exception is another common alternative in the Java API to returning a null when, for any reason, a value can't be provided. A typical example of this is the conversion of String into an int , provided by the Integer.


1 Answers

You can use an extension method - which you can generalize to any situation. This works because C# allows extension methods to work with null values for the this parameter (surprisingly!), whereas with normal instance methods you would otherwise get a NullReferenceException.

Here's something similar to what I used in my own projects before we had the ?. "safe-navigation" operator in C# 6:

public static class Extensions
{    
    public static TRet NullSafeCall<TValue,TRet>( this TValue value, Func<TValue,TRet> func )
        where TValue : class
        where TRet : class
    {
        if( value != null ) return func( value );
        return null;
    }
}

Used like so:

return p.NullSafeCall( callmethod );

It also supports the use of lambdas if you need to pass more than 1 argument into your subsequent func:

String foo = "bar";
return p.NullSafeCall( v => callmethod2( v, foo ) );

In your example you used String.IsNullOrEmpty instead of != null, which can be added like so:

public static class Extensions
{    
    public static TRet NullSafeCall<TValue,TRet>( this TValue value, Func<TValue,Boolean> guard, Func<TValue,TRet> func )
        where TValue : class
        where TRet : class
    {
        if( guard( value ) ) return func( value );
        return null;
    }
}

Usage:

return p.NullSafeCall( v => String.IsNullOrEmpty( v ), v => callmethod( v ) );

And of course, you can chain it:

return p
    .NullSafeCall( callmethod2 )
    .NullSafeCall( callmethod3 )
    .NullSafeCall( v => callmethod4( v, "foo", bar ) );
like image 126
Dai Avatar answered Sep 19 '22 21:09

Dai