C# 中与 NULL 的混淆

发布于 2024-07-16 06:47:46 字数 333 浏览 6 评论 0原文

我总是对表达空值的不同方式感到困惑。 有空引用类型(又名“null”)。 然后我发现在我的应用程序中,开发人员使用 MinValue 来表示空值。 例如:Double.MinValue 或 DateTime.MinValue,但它们使用“null”的字符串除外 然后

还有 System.DBNull(和 System.DBNull.Value - 不确定何时使用什么)。 更令人困惑的是,还有 System.NullableSystem.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 技术交流群。

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

发布评论

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

评论(10

浊酒尽余欢 2024-07-23 06:47:46

当然。

System.DBNull 是 ADO.NET 曾经(并且仍然)使用的一个类,用于表示数据库中的 null 值。

null 实际上是一个 null 引用,在您的应用程序代码中,任何引用类型都应该使用 null 作为其 null代码>值。

对各种基元类型使用 MinValue(由于它们是值类型,因此不能分配 null)可以追溯到引入泛型的 C# 2.0 之前的黑暗时期。 现在,表示可为 null 的基本类型的首选方法是使用 Nullable 泛型类型,通常用简写形式表示,在基本类型后面加一个问号。 例如,我可以在 C# 中将名为 foo 的可空 int 变量声明为:

Nullable<int> foo;

int? foo;

两者相同。

Sure.

System.DBNull is a class that was (and still is) used by ADO.NET to represent a null value in a database.

null is actually a null reference, and in your application code any reference type should use null as its, well, null value.

The usage of MinValue for various primitive types (which, since they are value types cannot be assigned null) 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 the Nullable<T> generic type, usually represented in shorthand with a question mark after the primitive type. For example, I could declare a nullable int variable named foo in C# as:

Nullable<int> foo;

or

int? foo;

Both are identical.

小情绪 2024-07-23 06:47:46
  • null 仅对引用类型有效:属于而不是结构的类型。
  • .Net 也有值类型:int、double、DateTime 等。值类型不能为 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 a class rather than a structure.
  • .Net also has value types: int, double, DateTime, etc. Value types cannot be null, you so you normally compare those to their default value, which is very often 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 same Nullable<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.
  • When using generics, you'll also see the 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.
假扮的天使 2024-07-23 06:47:46

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 a Null, 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.

笑咖 2024-07-23 06:47:46
  • null 对于引用类型
    实际的空值
  • Nullable,在.NET 2.0中添加的是
    表达值的空值的方式
    类型,根据定义不能是
    无效的。
  • 与 DateTime.MinValue 相同 -
    DateTime 是一个值类型,不能
    为空,这样你就可以有一个约定
    一个众所周知的值,比如
    DateTime.MinValue 被视为好像
    为空。 它还有其他用途。
  • null for reference types in the
    actual null value
  • Nullable, added in .NET 2.0 is a
    way of expressing nulls for value
    types, which by definition can not be
    null.
  • Same with DateTime.MinValue -
    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.
你的往事 2024-07-23 06:47:46

在 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 a DateTime? date = null than DateTime 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.

花伊自在美 2024-07-23 06:47:46

最小值不为空。 这是最小值。 使用 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".

梨涡 2024-07-23 06:47:46

引用类型(也称为对象)可以设置为 null,因为引用类型变量只是指向实际实例的指针。 指示缺少实例很容易,因为您可以直接将变量设置为 null。

对于值类型来说,这有点困难,因为值类型变量总是包含一个值。
因此,在 Nullable 出现之前,使用 Double.MinValue 或 DateTime.MinValue 在某种程度上是有效的。 当时没有简单的方法来表达值类型中缺少值。

现在,对于可为 null 的类型,您可以说:

double? d = 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:

double? d = null;

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.

风苍溪 2024-07-23 06:47:46

.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 using DateTime.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 of NULL 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 using MinValue, but was added in .NET 2, so older designs may have started using MinValue before Nullable<T> is available.

梦一生花开无言 2024-07-23 06:47:46

tou 理解这些不同的 null 值的最佳方法是尝试它们并查看每个值的用途,但为了让事情变得简单,这里有一些指导原则

  • null 只能用于引用类型,如集合和自定义类(字符串是特殊的)
  • DBNull 用于来自数据库查询的空值。
  • 因为我们无法将 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

  • null can be used only on reference types like collections and custom classes (string is special)
  • DBNull is for null values that came out of the db query.
  • 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.

柠檬 2024-07-23 06:47:46

这里的重要区别是值类型和引用类型之间的区别。
值类型直接表示某个值,而引用类型则指向应表示值的内存位置。 当引用类型实际上没有指向具有有效内容的内存位置时,该引用为 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.

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