简体   繁体   中英

What does the bool? return type mean?

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

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.

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.

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 .

In C#, the "primitive" types can't hold a null value:

int blah = null;

That will make an exception because 'int' can't hold a null value.

Same with 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).

How do you hold that row on an object? Your int cannot hold the null value and the row can have 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:

Nullable<int> blah = null;

That will not make an exception, now, that variable can hold an int and a null.

Because Nullable is too long, MS created some kind of alias: If you put the '?' 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 ? 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.
  • ...

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.

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:

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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