Mandar Jogalekar
Mandar Jogalekar

Reputation: 3281

Null parameter alternative 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?

Upvotes: 8

Views: 2967

Answers (4)

Dai
Dai

Reputation: 155708

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

Upvotes: 8

hsoesanto
hsoesanto

Reputation: 523

AFAIK using the if statement is good as it expresses your intent quite clearly. An alternative approach, alleviating this feature is by creating an extension method on the string "callmethod"

public static class extension
{
     public static void callmethod (this string myInput)
     {}
}

and as such, you would be able to do

p?.callmethod();

Upvotes: 2

Mohammad Aminul Islam
Mohammad Aminul Islam

Reputation: 86

Without 'if else', you can do that by using following code-

p!=string.Empty?callmethod(p):"";

Upvotes: 0

Samvel Petrosov
Samvel Petrosov

Reputation: 7706

About Null-conditional operators you can read here.

The alternative for parameters' checking for null is the ?? operator which is used in the following way:

someMethod(val??"0");// this means that "0" will be passed as a value if val is null

But this is no way connected to checking for empty string. So you will have to check for empty string anyway if the value is not allowed to be it.

Upvotes: 2

Related Questions