简体   繁体   English

什么是布尔? 返回类型意味着什

[英]What does the bool? return type mean?

I saw a method return bool? 我看到一个方法返回bool? , does anyone know the meaning of it? ,有谁知道它的含义?

T? is a C# syntax shortcut to Nullable<T> . Nullable<T>的C#语法快捷方式。 So bool? 那么bool? maps to Nullable<bool> . 映射到Nullable<bool> So in your case it's a struct that either is null , or has a bool value. 所以在你的情况下,它是一个结构,它可以是null ,或者具有bool值。

If a Nullable<T> is null that's a bit different from a reference type being null. 如果Nullable<T>null ,则与引用类型为null有点不同。 It basically is a struct containing the underlying type and a boolean flag HasValue . 它基本上是一个包含底层类型和布尔标志HasValue But both the runtime and the C# language have a bit of magic to pretend that a Nullable with HasValue==false is really null. 但是运行时和C#语言都有点神奇,假装具有HasValue==falseNullable实际上是空的。 But the difference still leaks sometimes. 但有时候差异仍然存在。

The underlying type is implicitly convertible to the nullable type ( bool -> bool? ). 底层类型可以隐式转换为可空类型( bool - > bool? )。 To get the underlying type from the nullable type, you can either cast explicitly ( (bool)myNullableBool or use the Value property ( myNullableBool.Value ). You should check for null before though, or you'll get an exception if the nullable value is null . 要从可空类型中获取基础类型,您可以显式转换( (bool)myNullableBool或使用Value属性( myNullableBool.Value )。您应该在之前检查null ,否则如果可以为null值,您将获得异常是null

In C#, the "primitive" types can't hold a null value: 在C#中,“原始”类型不能包含空值:

int blah = null;

That will make an exception because 'int' can't hold a null value. 这会产生异常,因为'int'不能保存null值。

Same with double, float, bool, DateTime... 与double,float,bool,DateTime相同......

That is fine really. 真的很好。 The problem comes when you are using databases. 使用数据库时会出现问题。 In a database you can have rows that can be null, like Age (that is an int). 在数据库中,您可以拥有可以为null的行,例如Age(即int)。

How do you hold that row on an object? 你如何在一个物体上持有那一行? Your int cannot hold the null value and the row can have null. 你的int不能保存null值,行可以为null。

For that reason MS created a new struct, the Nullable, you can pass to that struct a "primitive" type (or a struct) to make it nullable like: 出于这个原因,MS创建了一个新的结构,Nullable,你可以将一个“原始”类型(或结构)传递给该结构,使其可以为空:

Nullable<int> blah = null;

That will not make an exception, now, that variable can hold an int and a null. 现在,这不会成为例外,该变量可以包含int和null。

Because Nullable is too long, MS created some kind of alias: If you put the '?' 因为Nullable太长了,MS创造了某种别名:如果你把'?' after the type, it becomes nullable: 在类型之后,它变得可以为空:

int? blah = null;
Nullable<int> blah = null;

This two lines are EXACTLY the same but the first one is easier to read. 这两行完全相同,但第一行更容易阅读。

Something like that :) 这样的东西:)

Any (no-nonsense 1 ) value type suffixed with ? 任何(非废话1值类型后缀? makes it a nullable type: 使它成为可以为空的类型:

  • int? is a nullable integer 是一个可以为空的整数
  • bool? is nullable boolean which makes is actually a triple state boolean ( true , false and null ) - think of it when you need to implement some triple state behaviour. 是可以为空的布尔值,它实际上是一个三态布尔值truefalsenull ) - 当你需要实现一些三态行为时想一想。
  • ... ...

In other words object type doesn't support nullable definition as object? 换句话说, object类型不支持可空定义作为object? because it's reference type and can have a value of null already. 因为它是引用类型,并且可以具有null值。

Real-life usage 真实的用法

Nullable types are most often used against database, because they make it really easy to transform nullable int/bit/datetime/etc. Nullable类型最常用于数据库,因为它们可以很容易地转换为nullable int / bit / datetime / etc. columns to CLR types. 列到CLR类型。 Like in this example: 就像在这个例子中:

create table Test
(
    Id int identity not null primary key,
    Days int null,
    ExactDate datetime null,
    Selected bit null
)

This can easily be translated to POCO: 这很容易转换为POCO:

public class Test
{
    public int Id { get; set; }
    public int? Days { get; set; }
    public DateTime? ExactDate { get; set; }
    public bool? Selected { get; set; }
}

1 no-nonsense refers to all value types except Nullable<T> which is a value type per se, but it wouldn't make any sense in making it nullable once more... Avoid such stupidity... It won't compile anyway because Nullable<Nullable<int>> i = null; 1 no-nonsense是指除Nullable<T>之外的所有值类型,它本身就是一个值类型,但是再次使它可以为空可能没有任何意义...避免这样的愚蠢......它不会编译无论如何因为Nullable<Nullable<int>> i = null; can be interpreted in two ways: 可以用两种方式解释:

i.HasValue == false i.HasValue == false
i.HasValue == true && i.Value.HasValue == false i.HasValue == true && i.Value.HasValue == false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM