在扩展方法中处理null [英] Handling null in extension method

查看:97
本文介绍了在扩展方法中处理null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的字符串类扩展方法,它将从字符串中剥离所有非数字字符.因此,如果我有一个字符串,例如电话号码,例如((555)215-4444)",它将把它转换为"5552154444".看起来像这样:

I have a simple extension method for string class which will strip all non numeric characters from a string. So if I have a string like for example a phone number such as "(555) 215-4444" it will convert it to "5552154444". It looks like this:

public static string ToDigitsOnly(this string input)
{
    Regex digitsOnly = new Regex(@"[^\d]");
    return digitsOnly.Replace(input, String.Empty);
}

我只是想知道在这里处理null值的最优雅方法是什么?在这些情况下,是否有一种典型的模式可以遵循,例如,如果传入null,则返回null值?似乎因为我在这里扩展字符串类,所以我可能想允许空值并且不引发争论异常(因为当我使用this时我并没有真正传递争论...)?但是有些人可能会认为我应该像正常"方法一样抛出异常.您在这里使用的最佳做法是什么?

I am just wondering what is the most elegant way to handle a null value here? Is there a typical pattern to follow in these cases, such as return back a null value if a null is passed in? It seems since I'm extending the string class here I may want to allow null values and not throw a arguement exception (since I'm not really passing in an arguement when I use this...) ? But some might argue I should throw an exception like a 'normal' method would. What's the best practice you are using here?

谢谢!

推荐答案

您可以遵循最不惊奇的原则:使用LINQ中实现的模式:

You can follow the principle of least surprise: use pattern implemented in LINQ:

public static string ToDigitsOnly(this string input)
{
    if(input == null)
          throw new ArgumentNullException("input");

    Regex digitsOnly = new Regex(@"[^\d]");
    return digitsOnly.Replace(input, String.Empty);
}

您可以使用方法

You can use method, proposed by Jon Skeet. It will reduce your check simply to

input.ThrowIfNull("input");

乔恩(Jon)也有很好的部分, 10.2.4在 C#深度中的空引用上调用方法,引用:

Also Jon has a good section 10.2.4 Calling a method on a null reference in C# in Depth, quote:

检查是否为空作为一个认真的开发人员,我确定您的 生产方法总是在检查其论点之前是否有效 进行中.一个奇怪的问题自然而然地产生了 扩展方法的功能是在第一个时抛出什么异常 参数为null(假设它不是本意).应该是 ArgumentNullException,就好像它是普通参数一样,还是应该 是NullReferenceException,如果 扩展方法曾经是一个实例方法?一世 建议前者:即使扩展名仍然是一个论点 方法语法并没有那么明显.

CHECKING FOR NULLITY As a conscientious developer, I’m sure that your production methods always check their arguments’ validity before proceeding. One question that naturally arises from this quirky feature of extension methods is what exception to throw when the first argument is null (assuming it’s not meant to be). Should it be ArgumentNullException, as if it were a normal argument, or should it be NullReferenceException, which is what would’ve happened if the extension method had been an instance method to start with? I recommend the former: it’s still an argument, even if the extension method syntax doesn’t make that obvious.

我认为此建议是(并根据我的个人经验):检查空值总是更好,特别是对于静态方法,并且不要依赖空值.仅当它是方法的确切用途(例如ThrowIfNullIsNullOrEmpty扩展方法)时例外.

I see this recommendation as (and from my personal experience): it's always better to check for null, specially for static methods and do not to rely on null values. One exception only if it is the exact purpose of your method, for example ThrowIfNull or IsNullOrEmpty extension methods.

这篇关于在扩展方法中处理null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆