我在函数参数中找到了这个语句。
public static SyndicationFeed GetDataFeed(...... int? maxResults)
这意味着什么?
答案 0 :(得分:11)
INT?意味着它是一个可以为空的int,因此不仅允许整数值,还允许空值。
答案 1 :(得分:7)
它是Nullable Type,Nullable<T>
。根据{{3}}
DataType int
基本上是一个值类型,不能保存空值,但是通过使用Nullable Type,您实际上可以在其中存储null。
maxResults = null; // no syntax error
您可以使用两种语法声明Nullable Types
:
int? maxResults;
OR
Nullable<int> maxResults;
答案 2 :(得分:2)
?
表示可以为空。这意味着类型可以包含值或为null。
int? num = null; // assign null to the variable num
http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.100).aspx
答案 3 :(得分:2)
这是Nullable<int>
的快捷方式 - 这意味着maxResults
可以指定为空。
答案 4 :(得分:2)
代表int type that can be assigned null.
答案 5 :(得分:1)
通常,int不能具有 null 值,但是使用'?'前缀允许它为Nullable:
int? myNullableInt = null; //Compiles OK
int myNonNullableInt = null; //Compiler output - Cannot convert null to 'int' because it is a non-nullable value type
int myNonNullableInt = 0; //Compiles OK
在您的问题/代码的上下文中,我只能假设它负责根据?maxResults 的值返回 SyndicationFeed 结果,但是因为它可以为空值可以为null。
答案 6 :(得分:1)
您无法将null
分配给包含integer
的任何值类型。您将获得例外
int someValue;
someValue=null;//wrong and will not work
但是当你使它可以为空时,你可以指定null。 要使ValueType为Nullable,您必须使用符号?
来关注您的valuetype关键字<type>? someVariable;
int? someValue;
someValue=null;//assigns null and no issues.
现在你不会得到异常,你可以指定null。