我在一次采访中被问及如何从具有整数、 double 类型等的方法返回 Null,而没有任何输出参数。
最佳答案
你可以这样做。
你必须首先使 int
类型可以为空。通过使用 int?
通常 c# 中的 int
数据类型默认情况下不可为空,因此您必须将 int//not nullable
类型显式转换为 int?//可空
你可以用 double 等做同样的事情。
// the return-type is int?. So you can return 'null' value from it.
public static int? method()
{
return null;
}
上面的方法也可以这样写:
// this is another way to convert "non-nullable int" to "nullable int".
public static Nullable<int> method()
{
return null;
}
关于c# - 如何从应返回整数的函数中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53852117/