C# 中与 NULL 的混淆
我总是对表达空值的不同方式感到困惑。 有空引用类型(又名“null”)。 然后我发现在我的应用程序中,开发人员使用 MinValue 来表示空值。 例如:Double.MinValue 或 DateTime.MinValue,但它们使用“null”的字符串除外 然后
还有 System.DBNull(和 System.DBNull.Value - 不确定何时使用什么)。 更令人困惑的是,还有 System.Nullable
和 System.Nullable
命名空间。
有人可以帮助我消除这种空混乱吗?
谢谢
I am always confused with the different ways of expressing nulls. There is the null reference type (aka "null"). Then I've seen that throughout my application, developers have used MinValue to represent nulls. Ex: Double.MinValue or DateTime.MinValue except for a String for which they use "null"
Then there is System.DBNull (and System.DBNull.Value - not sure what to use when). To add to the confusion, there are also System.Nullable
and System.Nullable<T>
namespaces.
Can someone help me clear this null confusion?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
当然。
System.DBNull
是 ADO.NET 曾经(并且仍然)使用的一个类,用于表示数据库中的null
值。null
实际上是一个null
引用,在您的应用程序代码中,任何引用类型都应该使用null
作为其null
代码>值。对各种基元类型使用
MinValue
(由于它们是值类型,因此不能分配null
)可以追溯到引入泛型的 C# 2.0 之前的黑暗时期。 现在,表示可为 null 的基本类型的首选方法是使用Nullable
泛型类型,通常用简写形式表示,在基本类型后面加一个问号。 例如,我可以在 C# 中将名为foo
的可空int
变量声明为:或
两者相同。
Sure.
System.DBNull
is a class that was (and still is) used by ADO.NET to represent anull
value in a database.null
is actually anull
reference, and in your application code any reference type should usenull
as its, well,null
value.The usage of
MinValue
for various primitive types (which, since they are value types cannot be assignednull
) dates back to the dark days before C# 2.0, which introduced generics. Now the preferred method for representing a nullable primitive type is to use theNullable<T>
generic type, usually represented in shorthand with a question mark after the primitive type. For example, I could declare a nullableint
variable namedfoo
in C# as:or
Both are identical.
null
仅对引用类型有效:属于类
而不是结构
的类型。type.MinValue
,但也可能是某些东西(例如,考虑布尔值)。Nullable
适用于您的值类型可能确实为 null 的情况。 默认值(可能是 MinValue)也是有效的,您需要将其与变量尚未分配时区分开来。 在 C# 中,您可以使用 ? 使用值类型作为简写符号(例如:int?
),但您仍然创建相同的Nullable
。DBNull
特指数据库中的 NULL 值。 它与语言中其他地方使用 null 不同:它仅用于与数据库通信,以便您可以知道查询何时返回 null 值。default(T)
构造,其中 T 是类型参数。 这允许您在不知道该类型是引用类型还是值类型的情况下设置类型的默认值,更不用说特定值类型的默认值可能是什么了。null
is only valid for reference types: types that are aclass
rather than astructure
.type.MinValue
, but might be something (consider Boolean, for example).Nullable<T>
is for when you have a value type that might genuinely be null. The default value (maybe MinValue) is also valid and you need to distinguish it from when the variable has not been assigned yet. In C#, you can use a ? with a value type as a short hand notation (for example:int?
), but you're still creating the sameNullable<T>
.DBNull
specifically refers to NULL values from a database. It's not the same thing as using null elsewhere in the language: it's only for talking to a database so you can know when a query returned a null value.default(T)
construct used, where T is a type parameter. This allows you to set a type's default value without knowing whether that type is a reference type or a value type, much less what a specific value type's default value might be.Null
值表示尚未分配内存的任何引用类型对象。MinValue
并不表示Null
,而是一个经常用于表示给定值类型可以具有的最小可能常量值的属性。DBNull.Value
类是返回/传递到数据库的 Null 的映射。Nullable 泛型类型使您能够将 Null 值分配给值类型。
A
Null
value represents any reference type object that has not had its memory allocated yet.The
MinValue
does not represent aNull
, rather it is a property that is often used to represent the smallest possible constant value that a given value type can have.The
DBNull.Value
class is a mapping of the Nulls returned/passed to a database.The Nullable generic type enables you to assign Null values to a Value-type.
实际的空值
表达值的空值的方式
类型,根据定义不能是
无效的。
DateTime 是一个值类型,不能
为空,这样你就可以有一个约定
一个众所周知的值,比如
DateTime.MinValue 被视为好像
为空。 它还有其他用途。
actual null value
way of expressing nulls for value
types, which by definition can not be
null.
DateTime is a value type, and can not
be null, so you can have a convention
that a well known value, like
DateTime.MinValue is treated as if it
was null. It also has other usages.
在 C# 2.0 中可空类型出现之前,“
MinValue
”与值类型一起使用。 因此,有很多遗留代码使用旧的方式来了解值类型何时没有值。 现在使用DateTime 更容易吗? date = null
大于DateTime date = DateTime.MinValue
。 就 DBNull 而言,这是数据库抽象层所必需的,但您可以通过使用诸如 NHibernate 之类的 ORM 来避免自己处理它 - 您几乎可以从应用程序中处理它。 从开发的角度来看,只需处理内置的 C# 类型。"
MinValue
"s were used with value types before nullable types came around in C# 2.0. So there is a lot of legacy code which uses the old style of knowing when a value type doesn't have a value. Nowadays it's much easier to use aDateTime? date = null
thanDateTime date = DateTime.MinValue
. As far as DBNull goes, that is something that is required as an abstraction layer to databases, but you can avoid having to deal with it yourself by employing an ORM such as NHibernate or some such - you pretty much, from a app. development standpoint will only have to deal with built-in C# types.最小值不为空。 这是最小值。 使用 C# 1.0 的人有时将其用作“null”,因为 C# 1.0 没有可为 null 的值类型。 相反,他们应该使用 Nullable,而不是 DateTime?。
DBNull 是一个用于与数据库通信的 .NET 值(“DB”null)。 数据库概念 NULL 的含义与 .NET null 引用不同。 在数据库术语中,NULL 表示“未知”或“不存在”。
MinValue is not null. It's MinValue. It is sometimes used "as null" by people using C# 1.0, which did not have nullable value types. Instead, they should use Nullable, ot DateTime?.
DBNull is a .NET value that is used to communicate with a database ("DB" null). The database concept of NULL means something different from the .NET null reference. In database terms, NULL means "unknown" or "absent".
引用类型(也称为对象)可以设置为 null,因为引用类型变量只是指向实际实例的指针。 指示缺少实例很容易,因为您可以直接将变量设置为 null。
对于值类型来说,这有点困难,因为值类型变量总是包含一个值。
因此,在 Nullable 出现之前,使用 Double.MinValue 或 DateTime.MinValue 在某种程度上是有效的。 当时没有简单的方法来表达值类型中缺少值。
现在,对于可为 null 的类型,您可以说:
因此您也可以拥有包含 null 的值类型变量。
System.DBNull 是一个不同的故事,因为它直接与数据集中表达值“NULL”相关。 这是在可空类型之前引入的,在我看来,可空类型取代了 DBNull。
Reference types (aka objects) can be set to null since reference type variables is just a pointer to the actual instance. Indicating the lack of an instance is easy since you can set the variable to null directly.
For value types this is a bit harder since a value type variable always contains a value.
Therefore, the use of Double.MinValue or DateTime.MinValue was somewhat valid in the pre-Nullable days. Back then there was no easy way of expressing the lack of a value in value types.
Now, with nullable types you can say:
And thus you can also have value type variables containing null.
System.DBNull is a different story since it is directly tied to expressing the value "NULL" in datasets. This was introduced before nullable types which imo supersedes DBNull.
.NET 中的大多数类型都是引用类型,
null
是“不是引用”,可以将其分配给变量或字段以指示不存在对象。.NET 中的所有其他类型都是值类型。 总是有一个具有值类型的值,因此无法指示“无值”。 因此,实现可以定义特定的特殊值来指示这一点。 通常,值类型的
MinValue
(常量)字段用于此目的。 因此,使用DateTime.MinValue
作为出生日期可以表示“未知”或“不适用”(例如对于公司实体)。DbNUll
的存在是为了表达 RDBMS 的NULL
含义,这与 .NET 的含义略有不同。 在大多数情况下,这将被转换为 null 或类似的值。最后,
Nullable
是值类型的包装器,可以更直接地表达“未知”,通常是比使用MinValue
更好的选择,但在 . NET 2,因此较旧的设计可能在Nullable
可用之前就开始使用MinValue
。Most types in .NET are reference types, and
null
is "not a reference" which can be assigned to a variable or field to indicate there is no object.All other types in .NET are value types. There is always a value with a value type, so no ability to indicate "no value". Therefore an implementation may define specific special values to indicate this. Often the value type's
MinValue
(constant) field is used for this. So usingDateTime.MinValue
for a date of birth can indicate "not known" or "not applicable" (e.g. for a corporate entity).DbNUll
exists to express RDBMS's meaning ofNULL
which is a little different to .NET's. In most cases this will be translated to null or similar.Finally,
Nullable<T>
is a wrapper for value types to more directly express "not known", and is generally a better option than usingMinValue
, but was added in .NET 2, so older designs may have started usingMinValue
beforeNullable<T>
is available.tou 理解这些不同的 null 值的最佳方法是尝试它们并查看每个值的用途,但为了让事情变得简单,这里有一些指导原则
因为我们无法将 null 分配给小数或双精度,所以我们使用 MinValue 属性分配它们(它们是值对象)
因为
Nullable 是开发人员将 null 值分配给值对象(如十进制)的一种方法,
可空; 与 int 相同?
我希望我能帮助您理解其中的差异。
the best way for tou to understand this different null values is to try them and see what the use of each one but to make things simple here is some guideline
because we cant assign null to a decimal or double we assign them with the MinValue property (they are value objects)
Nullable is a way for the developer to assign null values to value objects like decimal,
Nullable<int> is same as int?
i hope i helped you understanding the differences.
这里的重要区别是值类型和引用类型之间的区别。
值类型直接表示某个值,而引用类型则指向应表示值的内存位置。 当引用类型实际上没有指向具有有效内容的内存位置时,该引用为 Null。
由于值类型是值的直接表示,因此它们不能为 null。 然而,可能存在值类型的实际值未知的情况。 对于这种情况,.Net 提供了 Nullable 类型构造。 然而,当这样的构造不可用时,人们倾向于使用特殊值或默认值,例如 MinValue。
当与数据库通信时,我们期望的许多值类型实际上可以为 Null,因为这是数据库处理未知值的方式。 这也可以通过 Nullable 类型来解决,但这些类型并不总是可用。 这就是 DBNull 存在的原因,以处理数据库中可能出现的空值。
The important difference here is that between value types and reference types.
Value types represent directly a certain value, whereas reference types point to a memory location that should represent a value. When a a reference type actually does not point to a memory location with valid contents, the reference is Null.
Since value types are direct representations of value, they cannot be null. However, it might be the case that a actual value of a value type is unknown. For this case, .Net provides the Nullable types construct. When such a construct is not available, however, people tend to use special or default values, such as MinValue.
When communicating with databases, a lot of things we expect to be value types can actually be Null, since that it the way the database handles unknown values. This can also be solved by the Nullable types, but these were not always available. That's why DBNull exist, to deal with a possible null in a database.