简体   繁体   中英

c# “?” in method declaration

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.

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.

int? = the value can be integer or null

int? is a type and is equivalent to Nullable<int> . This type can store an Integer or Null.

int? is the shorthand for Nullable<int> More info here: Nullable ᐸTᐳ Structure

It allows you to have "null" values in your int. More info here: Using Nullable Types

这是定义可空类型的语法快捷方式,通常使用Nullable<T>类型定义

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