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

时间:2022-11-07 17:02:17

I saw a method return bool?, does anyone know the meaning of it?

我看到一个方法返回bool?,有谁知道它的含义?

3 个解决方案

#1


25  

T? is a C# syntax shortcut to Nullable<T>. So bool? maps to Nullable<bool>. So in your case it's a struct that either is null, or has a bool value.

T'是Nullable 的C#语法快捷方式。那么布尔?映射到Nullable 。所以在你的情况下,它是一个结构,无论是null,还是具有bool值。

If a Nullable<T> is null that's a bit different from a reference type being null. It basically is a struct containing the underlying type and a boolean flag 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. But the difference still leaks sometimes.

如果Nullable 为null,则与引用类型为null有点不同。它基本上是一个包含底层类型和布尔标志HasValue的结构。但是运行时和C#语言都有点神奇,假装具有HasValue == false的Nullable实际上是空的。但有时候差异仍然存在。

The underlying type is implicitly convertible to the nullable type (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-> bool?)。要从可空类型中获取基础类型,您可以显式转换((bool)myNullableBool或使用Value属性(myNullableBool.Value)。您应该在之前检查null,否则如果可空值可以获得异常一片空白。

#2


12  

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 '?' after the type, it becomes nullable:

因为Nullable太长,MS创建了某种别名:如果你把'?'在类型之后,它变得可以为空:

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 :)

像这样:)

#3


5  

Any (no-nonsense1) value type suffixed with ? makes it a nullable type:

任何(no-nonsense1)值类型后缀为?使它成为可以为空的类型:

  • 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.
  • 布尔?是可以为空的布尔值,它实际上是一个三态布尔值(true,false和null) - 当你需要实现一些三态行为时想一想。

  • ...

In other words object type doesn't support nullable definition as 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. columns to CLR types. Like in this example:

Nullable类型最常用于数据库,因为它们使得转换为nullable int / bit / datetime /等非常容易。列到CLR类型。就像在这个例子中:

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; }
}

1no-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; can be interpreted in two ways:

1 no-nonsense指的是除了Nullable 之外的所有值类型,它本身就是一个值类型,但是再次使它可以为空可能没有任何意义...避免这样的愚蠢...它不会编译无论如何因为Nullable > i = null;可以用两种方式解释:

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

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

#1


25  

T? is a C# syntax shortcut to Nullable<T>. So bool? maps to Nullable<bool>. So in your case it's a struct that either is null, or has a bool value.

T'是Nullable 的C#语法快捷方式。那么布尔?映射到Nullable 。所以在你的情况下,它是一个结构,无论是null,还是具有bool值。

If a Nullable<T> is null that's a bit different from a reference type being null. It basically is a struct containing the underlying type and a boolean flag 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. But the difference still leaks sometimes.

如果Nullable 为null,则与引用类型为null有点不同。它基本上是一个包含底层类型和布尔标志HasValue的结构。但是运行时和C#语言都有点神奇,假装具有HasValue == false的Nullable实际上是空的。但有时候差异仍然存在。

The underlying type is implicitly convertible to the nullable type (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-> bool?)。要从可空类型中获取基础类型,您可以显式转换((bool)myNullableBool或使用Value属性(myNullableBool.Value)。您应该在之前检查null,否则如果可空值可以获得异常一片空白。

#2


12  

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 '?' after the type, it becomes nullable:

因为Nullable太长,MS创建了某种别名:如果你把'?'在类型之后,它变得可以为空:

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 :)

像这样:)

#3


5  

Any (no-nonsense1) value type suffixed with ? makes it a nullable type:

任何(no-nonsense1)值类型后缀为?使它成为可以为空的类型:

  • 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.
  • 布尔?是可以为空的布尔值,它实际上是一个三态布尔值(true,false和null) - 当你需要实现一些三态行为时想一想。

  • ...

In other words object type doesn't support nullable definition as 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. columns to CLR types. Like in this example:

Nullable类型最常用于数据库,因为它们使得转换为nullable int / bit / datetime /等非常容易。列到CLR类型。就像在这个例子中:

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; }
}

1no-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; can be interpreted in two ways:

1 no-nonsense指的是除了Nullable 之外的所有值类型,它本身就是一个值类型,但是再次使它可以为空可能没有任何意义...避免这样的愚蠢...它不会编译无论如何因为Nullable > i = null;可以用两种方式解释:

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

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