简体   繁体   English

方法声明中的c#“?”

[英]c# “?” in method declaration

I'm learning C# at the moment, and I have never seen this before. 我正在学习C#,我以前从未见过这个。

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. 我确实尝试在Google和SE上查找,但我真的不知道我应该搜索哪些关键术语。

The int? int? is a nullable int. 是一个可以为空的int。 Using this as the return type of your method means that this method returns either an int or null. 使用它作为方法的返回类型意味着此方法返回int或null。 According to MSDN 根据MSDN

Nullable types are instances of the System.Nullable struct. 可空类型是System.Nullable结构的实例。 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. 例如,可以为发音为“Nullable of Int32”的Nullable分配从-2147483648到2147483647的任何值,或者可以为其指定空值。

int? = the value can be integer or null =该值可以是整数或null

int? is a type and is equivalent to Nullable<int> . 是一个类型,相当于Nullable<int> This type can store an Integer or Null. 此类型可以存储Integer或Null。

int? is the shorthand for Nullable<int> More info here: Nullable ᐸTᐳ Structure Nullable<int>的简写。更多信息: NullableᐸTᐳ结构

It allows you to have "null" values in your int. 它允许你在int中有"null"值。 More info here: Using Nullable Types 更多信息: 使用Nullable类型

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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