Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the bool? return type mean?

Tags:

c#

.net

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

like image 340
user496949 Avatar asked Feb 21 '11 10:02

user496949


People also ask

What does bool function return?

A Boolean function is like a built-in function except that it returns a value of true or false instead of number, string, or date. The result of a Boolean function cannot be printed; it can only be used as a condition. A Boolean function is composed of a function name followed by an operand in parentheses.

What does bool () return in Python?

The bool() function returns the boolean value of a specified object.

What does bool (' false ') return?

bool() Return Value False - if argument is empty, False, 0 or None. True - if argument is any number (besides 0), True or a string.

What is bool return C++?

A boolean data type is declared with the bool keyword and can only take the values true or false . When the value is returned, true = 1 and false = 0 .


3 Answers

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.

  • Nullable Types (C# Programming Guide)
  • Nullable<bool>
like image 59
CodesInChaos Avatar answered Sep 21 '22 09:09

CodesInChaos


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

like image 20
Jesus Rodriguez Avatar answered Sep 22 '22 09:09

Jesus Rodriguez


Any (no-nonsense1) 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; }
}

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:

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

like image 23
Robert Koritnik Avatar answered Sep 23 '22 09:09

Robert Koritnik