'int' 和有什么不一样?和 C# 中的“int”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/121680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between 'int?' and 'int' in C#?
提问by cori
I am 90% sure I saw this answer on stackoverflow before, in fact I had never seen the "int?" syntax before seeing it here, but no matter how I search I can't find the previous post, and it's driving me crazy.
我 90% 确定我之前在 stackoverflow 上看到过这个答案,事实上我从未见过“int?” 在这里看到它之前的语法,但无论我如何搜索,我都找不到以前的帖子,这让我发疯。
It's possible that I've been eating the funny mushrooms by accident, but if I'm not, can someone please point out the previous post if they can find it or re-explain it? My stackoverflow search-fu is apparently too low....
有可能是我不小心吃到了有趣的蘑菇,但如果我不是,有人可以指出以前的帖子,如果他们能找到或重新解释一下吗?我的 stackoverflow search-fu 显然太低了....
采纳答案by Forgotten Semicolon
回答by KyleLanser
回答by Charles Graham
int? is the same thing as Nullable. It allows you to have "null" values in your int.
内部?与 Nullable 是一回事。它允许您在 int 中有“空”值。
回答by Krishnaveni B
int belongs to System.ValueType and cannot have null as a value. When dealing with databases or other types where the elements can have a null value, it might be useful to check if the element is null. That is when int? comes into play. int? is a nullable type which can have values ranging from -2147483648 to 2147483648 and null.
int 属于 System.ValueType 并且不能将 null 作为值。在处理元素可以具有空值的数据库或其他类型时,检查元素是否为空可能很有用。那是什么时候int?发挥作用。内部?是可空类型,其值范围从 -2147483648 到 2147483648 和 null。
Reference: https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
回答by sanmatrix
the symbol ? after the int means that it can be nullable.
符号?在 int 之后意味着它可以为空。
The ? symbol is usually used in situations whereby the variable can accept a null or an integer or alternatively, return an integer or null.
这 ?符号通常用于变量可以接受空值或整数或返回整数或空值的情况。
Hope the context of usage helps. In this way you are not restricted to solely dealing with integers.
希望使用上下文有所帮助。通过这种方式,您不仅限于处理整数。
回答by Mohammad Taher Assad
you can use it when you expect a null value in your integer especially when you use CASTING ex:
当您期望整数中的空值时,您可以使用它,尤其是当您使用 CASTING ex 时:
x= (int)y;
if y = null then you will have an error. you have to use:
如果 y = null 那么你将有一个错误。你必须使用:
x = (int?)y;
回答by Fahad Jamal uddin
Int cannot accept null but if developer are using int? then you store null in int like int i = null; // not accept int? i = null; // its working mostly use for pagination in MVC Pagelist
Int 不能接受 null 但如果开发人员使用 int?然后将 null 存储在 int 中,例如 int i = null; // 不接受 int? 我 = 空;// 它的工作主要用于 MVC Pagelist 中的分页