c#中什么是可空类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13238059/
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 is nullable type in c#?
提问by tgranjith
when we have to use nullabletype in C#.net? could any one please explain with example.
当我们必须在 C#.net 中使用可空类型时?任何人都可以用例子解释一下。
回答by Pranay Rana
Nullable type is new concept introduced in C#2.0 which allow user to assingn null value to primitive data types of C# language. Important to not here is Nullable type is Structure type.
可空类型是 C#2.0 中引入的新概念,它允许用户将空值分配给 C# 语言的原始数据类型。重要的是这里不是 Nullable 类型是 Structure 类型。
BlogPost : Nullable type -- Why we need Nullable types in programming language ?
回答by Zzz
from: http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspxNullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value. For example, a Nullable<Int32>, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable<bool> can be assigned the values true or false, or null
来自:http: //msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80) .aspxNullable 类型是 System.Nullable 结构的实例。可空类型可以表示其基础值类型的正常值范围,外加一个额外的空值。例如,Nullable<Int32>,发音为“Nullable of Int32”,可以分配从 -2147483648 到 2147483647 的任何值,也可以分配空值。可以为 Nullable<bool> 分配值 true 或 false 或 null
class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
}
//y is set to zero
int y = num.GetValueOrDefault();
// num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}
回答by codingbiz
Nullable types (When to use nullable types)are value types that can take null as value. Its default is nullmeaning you did not assign value to it. Example of value types are int, float, double, DateTime, etc. These types have these defaults
可空类型(何时使用可空类型)是可以将 null 作为值的值类型。它的默认值null意味着您没有为其赋值。值类型的示例是 int、float、double、DateTime 等。这些类型具有这些默认值
int x = 0;
DateTime d = DateTime.MinValue;
float y = 0;
For Nullable alternatives, the defualt of any of the above is null
对于 Nullable 替代项,上述任何一项的默认值为 null
int? x = null; //no value
DateTime? d = null; //no value
This makes them behave like reference types e.g. object, string
这使它们的行为类似于引用类型,例如对象、字符串
string s = null;
object o = null;
They are very useful when dealing with values from database, when values returned from your table is NULL. Imagine an integer value in your database table that could be NULL, such can only be represented with 0if the c# variable is not nullable - regular integer.
它们在处理来自数据库的值时非常有用,当从表返回的值是NULL. 想象一下您的数据库表中的一个整数值可能为 NULL,只有0当 c# 变量不可为空时才能用它来表示- 常规整数。
Also, imagine an EndDatecolumn whose value is not determined until an actual time in future. That could be set to NULL in the DB but you'll need a nullable type to store that in C#
此外,想象一个EndDate列,其值直到将来的实际时间才确定。可以在数据库中将其设置为 NULL,但您需要一个可为空的类型来将其存储在 C# 中
DateTime StartDate = DateTime.Today;
DateTime EndDate? = null; //we don't know yet
回答by Anirudha
When we have to use nullable type in C#.net?
什么时候我们必须在 C#.net 中使用可空类型?
Imagine there is an integer variable idthat represents a particular id.
想象一下,有一个整数变量id代表一个特定的id.
You can store 1,44,or anything else.But what if you don't know the id.You cant just store -1or 0.You may be thinking to assign nullbut normally nullcannot be assigned to value types.
您可以存储1, 44, 或其他任何东西。但是如果您不知道 id 怎么办。您不能只存储-1或0。您可能正在考虑分配null但通常null不能分配给值类型。
int id=null;//error
Nullable typeenable you to do this.
Nullable type使您能够做到这一点。
Value types like int,double,char..cannotbe represented as NULLvalue.
像这样的值类型int,double,char..不能表示为NULL值。
To represent nullin value type you must use nullable type..
要null以值类型表示,您必须使用nullable type..
It is denoted as a value type followed by ?
它表示为值类型,后跟 ?
int? id=null;
int? id=null;
which is transalted to
转换为
Nullable<int> id=new Nullable<int>();
Nullable<int> id=new Nullable<int>();
The default value of it a nullable type is null.
可为空类型的默认值为null.

