简体   繁体   English

空参数替代C#6.0

[英]Null parameter alternative C# 6.0

I have seen new feature in C# 6 which allows code to skip if statements for null checks. 我已经看到C#6中的新功能允许代码跳过if语句进行null检查。

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 )? 如何调用p需要作为参数传递的方法(没有旧的if / else )?

The way I would normally write this with C# pre-6: 我通常用C#pre-6编写的方式:

 p != null ? callmethod(p) : null

is there something better in C# 6? 在C#6中有更好的东西吗?

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 . 这是有效的,因为C#允许扩展方法使用this参数的null值(令人惊讶!),而使用普通的实例方法,否则会得到NullReferenceException

Here's something similar to what I used in my own projects before we had the ?. 在我们拥有之前,这与我在自己项目中使用的内容类似?. "safe-navigation" operator in C# 6: 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 : 如果你需要将超过1个参数传递给后续的func ,它还支持使用lambdas:

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: 在您的示例中,您使用String.IsNullOrEmpty而不是!= null ,可以像这样添加:

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

About Null-conditional operators you can read here . 关于Null条件运算符,您可以在这里阅读。

The alternative for parameters' checking for null is the ?? 参数'检查null的替代方法是?? 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. 因此,如果不允许value则无论如何都必须检查空字符串。

AFAIK using the if statement is good as it expresses your intent quite clearly. 使用if语句的AFAIK很好,因为它非常清楚地表达了你的意图。 An alternative approach, alleviating this feature is by creating an extension method on the string "callmethod" 另一种减轻此功能的方法是在字符串“callmethod”上创建一个扩展方法

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

and as such, you would be able to do 因此,你可以做到

p?.callmethod();

没有'if else',你可以使用以下代码 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM