Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# "?" in method declaration

Tags:

c#

I'm learning C# at the moment, and I have never seen this before.

static int? Foo()
{
    return Bar
} 

What does the "?" do?

I did try looking it up on Google and SE but I don't really know what key terms I should be searching for.

like image 903
danielr Avatar asked Mar 03 '26 07:03

danielr


2 Answers

The int? is a nullable int. Using this as the return type of your method means that this method returns either an int or null. According to MSDN

Nullable types are instances of the System.Nullable struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, a Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value.

like image 122
Christos Avatar answered Mar 05 '26 20:03

Christos


int? = the value can be integer or null

like image 21
The scion Avatar answered Mar 05 '26 22:03

The scion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!