为什么不应该总是在C#中使用可空类型 [英] Why shouldn't I always use nullable types in C#

查看:155
本文介绍了为什么不应该总是在C#中使用可空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这方面的一些很好的指导,因为这个概念是在.NET 2.0中引入的。

I've been searching for some good guidance on this since the concept was introduced in .net 2.0.

为什么我会永远想使用非空的数据类型在C#? (一个更好的问题是,为什么不选我可空类型默认情况下,只有使用非可空类型时,明确是有道理的。)

Why would I ever want to use non-nullable data types in c#? (A better question is why wouldn't I choose nullable types by default, and only use non-nullable types when that explicitly makes sense.)

有一个'显著性能击中选择在其非空的同行一个可空数据类型?

Is there a 'significant' performance hit to choosing a nullable data type over its non-nullable peer?

我更喜欢检查我的价值观对空,而不是Guid.empty,的String.Empty, DateTime.MinValue,< = 0等,并与可空类型一般的工作。我不更经常选择可空类型的唯一理由就是在我的后脑勺发痒的感觉,让我觉得它比向后兼容性,迫使额外的'?'字,以明确允许空值了。

I much prefer to check my values against null instead of Guid.empty, string.empty, DateTime.MinValue,<= 0, etc, and to work with nullable types in general. And the only reason I don't choose nullable types more often is the itchy feeling in the back of my head that makes me feel like it's more than backwards compatibility that forces that extra '?' character to explicitly allow a null value.

有没有人在那里,总是(几乎总是)选择可空类型,而不是非可空类型?

Is there anybody out there that always (most always) chooses nullable types rather than non-nullable types?

谢谢您的时间,

推荐答案

为什么你不应该总是用可空类型的原因是,有时你能够保证值的将会的初始化。你应该尝试设计你的代码使这是常有的事成为可能。如果没有办法一个值可能被未初始化,那么没有理由空应该是它的合法值。作为一个非常简单的例子,考虑一下:

The reason why you shouldn't always use nullable types is that sometimes you're able to guarantee that a value will be initialized. And you should try to design your code so that this is the case as often as possible. If there is no way a value can possibly be uninitialized, then there is no reason why null should be a legal value for it. As a very simple example, consider this:

List<int> list = new List<int>()
int c = list.Count;

这是总是的有效。有没有可能的方式,其中 C 可能是未初始化。如果它变成了代码的 INT?,你会有效地告诉读者这个值可能为空。请一定要检查你使用它之前。但我们知道,这可能永远不会发生,那么为什么不公开此保证代码?

This is always valid. There is no possible way in which c could be uninitialized. If it was turned into an int?, you would effectively be telling readers of the code "this value might be null. Make sure to check before you use it". But we know that this can never happen, so why not expose this guarantee in the code?

您是绝对正确的情况下,一个值是可选的。如果我们有可能会或可能不会返回字符串的函数,则返回空。不要返回的String.Empty()。不要返回魔力值。

You are absolutely right in cases where a value is optional. If we have a function that may or may not return a string, then return null. Don't return string.Empty(). Don't return "magic values".

但并不是所有的值都是可选的。而且使一切可选使你的代码的其余部分更为复杂(它增加了一个必须处理的另一个代码路径)。

But not all values are optional. And making everything optional makes the rest of your code far more complicated (it adds another code path that has to be handled).

如果你能明确保证这个值将永远是有效的,那么,为什么扔掉这些信息?这是你通过它可空类型做什么。现在的值可能或可能不存在,并使用该值的人将必须处理这两种情况。但是你要知道,只有其中一个案件可能是摆在首位。所以,做你的代码的用户青睐,并反映在您的代码这一事实。那么你的代码的任何用户都可以依靠值是有效的,并且他们只需要处理一个单一的情况下,而不是两个。

If you can specifically guarantee that this value will always be valid, then why throw away this information? That's what you do by making it a nullable type. Now the value may or may not exist, and anyone using the value will have to handle both cases. But you know that only one of these cases is possible in the first place. So do users of your code a favor, and reflect this fact in your code. Any users of your code can then rely on the value being valid, and they only have to handle a single case rather than two.

这篇关于为什么不应该总是在C#中使用可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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