C# 我想要 "(int)null" 返回我 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2096619/
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
I want "(int)null" to return me 0
提问by Shantanu Gupta
How can i get 0 as integer value from (int)null.
我怎样才能从(int)null.
EDIT 1:I want to create a function that will return me default values for null representation in their respective datatypes.
编辑 1:我想创建一个函数,它将返回我各自数据类型中空表示的默认值。
EDIT 2:How can i work in this scenariofor using default.
编辑 2:我如何在这种情况下工作以使用default.
(int)Value
(int) 值
Where Value can be null or any integervalue. I dont know datatype of value at run time. But i will assure that Value should either contain null or Integer value only.
其中Value 可以是 null 或任何整数值。我不知道运行时值的数据类型。但我会保证 Value 应该只包含 null 或 Integer 值。
采纳答案by Pierre-Alain Vigeant
You can use the Nullable structure
您可以使用 Nullable 结构
int value = new Nullable<int>().GetValueOrDefault();
You can also use the defaultkeyword
您也可以使用default关键字
int value = default(int);
Following 2nd edit:
以下第二次编辑:
You need a function that receive any type of parameter, so object will be used. Your function is similar to the Field<T>extension method on a DataRow
您需要一个接收任何类型参数的函数,因此将使用 object。您的功能类似于Field<T>扩展方法上的DataRow
public static T GetValue<T>(object value)
{
if (value == null || value == DBNull.Value)
return default(T);
else
return (T)value;
}
Using that function, if you want an int (and you expect value to be an int) you call it like:
使用该函数,如果您想要一个 int(并且您希望值是一个 int),您可以像这样调用它:
int result = GetValue<int>(dataRow["Somefield"]);
回答by thecoop
You can't cast a nullto an int, as an int is a value type. You cancast it to an int?though.
您不能将 anull转换为 int,因为 int 是一种值类型。你可以将它转换为一个int?。
What is it you want to achieve?
你想达到什么目的?
回答by dtb
You can use the defaultkeyword to get the default value of any data type:
您可以使用default关键字获取任何数据类型的默认值:
int x = default(int); // == 0
string y = default(string); // == null
// etc.
This works with generic parameters as well:
这也适用于通用参数:
Bar<T> Foo<T>() {
return new Bar<T>(default(T));
}
In case you have a variable of type objectthat may contain nullor a value of type int, you can use nullable typesand the ?? operatorto convert it safely to an integer:
如果您有一个object可能包含类型的变量null或一个类型的值int,您可以使用可为空类型和?? 运算符将其安全地转换为整数:
int a = 42;
object z = a;
int b = (int?)z ?? 0; // == 42
int c = (int?)null ?? 0; // == 0
回答by LorenzCK
A generic method that returns a cast instance of an object or the default value could be implemented as follows:
返回对象的强制转换实例或默认值的通用方法可以实现如下:
static T Cast<T>(object value) {
if (value is T)
return (T)value;
else
return default(T);
}
This way, using a valid value will yield the value itself:
这样,使用有效值将产生值本身:
int value = Cast<int>(4); //value = 4
and a null value will get the default:
空值将获得默认值:
int value = Cast<int>(null); //value = 0
Notice that since the method takes object as an argument, this will cause boxing when used with struct objects (like int).
请注意,由于该方法将 object 作为参数,因此当与 struct 对象(如 int)一起使用时,这将导致装箱。
回答by Joshua
A method of a generic class that returns nothing works:
不返回任何内容的泛型类的方法有效:
Class GetNull(Of T)
Public Shared Function Value() As T
Return Nothing
End Function
End Class
Debug.Print(GetNull(Of Integer).Value())
回答by hunter
Check out this post for a mostly complete method for writing an extension to do just this. My scenario is pulling data from a NameValueCollection.
查看这篇文章,了解编写扩展来实现这一点的最完整的方法。我的方案是从 NameValueCollection 中提取数据。
http://hunterconcepts.com/blog/Extensions_HttpRequestFormQueryString/
http://hunterconcepts.com/blog/Extensions_HttpRequestFormQueryString/
回答by Syed Umar Ahmed
You'll need a return type of Nullable(Of Integer).
您需要返回类型为Nullable(Of Integer).
回答by vaheeds
Use null-coalescing operatoror ??
使用空合并运算符或??
Example:
例子:
int? a = null;
return a ?? 0; //this will return 0
It's a new feature in C#. It returns the left-hand operand if the operand is not null; otherwise, it returns the right-hand operand.
这是 C# 中的一项新功能。如果操作数不为空,则返回左侧操作数;否则,它返回右边的操作数。

