检查“null”

发布于 2024-12-17 13:33:51 字数 480 浏览 2 评论 0原文

C# 是否有一些经验法则或编码约定约定来处理可能的 null 参数?

例如,我正在编写一个自定义方法,它检索 byte[] data 参数。

public static string ConvertDataToMyOwnAndCustomString(byte[] data) { ... }

现在 - 如果传递的 datanull,我该怎么办?

我应该保持原样,以便可能出现 NullReferenceException< /代码>发生?或者我应该写一张支票并做这样的事情:

if (data == null) return null;

Does C# has some rule of thumb or a coding convention contract, which handles the possible null argument?

As an example, I'm writing a custom method, which retrieves a byte[] data parameter.

public static string ConvertDataToMyOwnAndCustomString(byte[] data) { ... }

Now - what should I do if the passed data is null?

Should I leave it as it is so that a possible NullReferenceException occurs? Or should I write a check and do something like this:

if (data == null) return null;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恏ㄋ傷疤忘ㄋ疼 2024-12-24 13:33:51

则普遍接受的模式是抛出 ArgumentNullException

if(data == null) { throw new ArgumentNullException("data"); }

如果客户端将 null 传递给您的方法确实是一个错误, 。如今,您甚至可以通过代码契约来强制执行该要求:

Contract.Requires(data != null);

当然,可能很可能 null 对于您的方法来说不是错误。不过,这由您决定,而不是我们。但不要返回 null 来向您的客户端表明发生了错误。

The generally accepted pattern is to throw an ArgumentNullException

if(data == null) { throw new ArgumentNullException("data"); }

if it is indeed an error for clients to be passing null to your method. These days, you can even enforce the requirement through a Code Contract:

Contract.Requires(data != null);

Of course, it might very well be the case that null is not an error for your method. That's for you to decide though, not us. But don't return null to indicate to your client that an error occurred.

后知后觉 2024-12-24 13:33:51

我认为最常见的模式是:

if(data == null) 
    throw new ArgumentNullException("data");

I think the most common pattern is:

if(data == null) 
    throw new ArgumentNullException("data");
尤怨 2024-12-24 13:33:51

这真的完全取决于。这个方法在哪里?多久重复使用一次?它仅适用于这个应用程序,还是它是将再次使用的代码库的一部分?

如果要使用它,特别是由其他应用程序/开发人员使用,那么我仍然会测试数据是否为 ​​null,但随后我会抛出异常。你不希望它悄无声息地窒息。

如果这是一种一次性方法,用于肯定永远不会被重用或重新访问的代码,您可以只测试 null 并返回 null。但请记住,只有当您知道结果时,这才是合适的。如果其他应用程序或其他开发人员有机会使用它,我会测试 null 然后抛出异常(很可能是 ArgumentNullException)。

It really all depends. Where does this method live? How often will it be reused? Is it only for this application, or is it part of a code library that will be utilized again?

If it is going to be used, especially by other applications/developers then I would still test for data being null, but then I would throw an exception. You don't want it to choke silently.

If this is a one-off method that is for code that will surely never be reused or revisited, you could just test for null and return null. Remember, though, this is only appropriate because you know of the outcome. If there is any chance some other application will use this, or another developer, I'd test for null then throw an exception (most likely an ArgumentNullException).

温馨耳语 2024-12-24 13:33:51

杰森和克里斯托弗的答案是正确的。我只是想补充一下;

如果您可能会为该参数获取 null,但不是异常情况,则不应抛出异常,而应返回适当的值 - 通常为 null本身有效。

例如,对于某些模型绑定系统,缺少文本值可能会导致 null 作为参数传递。这种情况可能是一个错误,但不是“异常”错误。

Jason and Christopher's answers are correct. I just want to add to them;

If it is likely and not an exceptional condition that you could get null for that parameter, you should not throw an exception, but instead return an appropriate value - very often, null itself works.

For example, with some systems of model binding, a missing text value might result in a null being passed as a parameter. Such an occurrence might be an error, but not an 'exceptional' one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文