return(Object)null在c#中是什么意思?

时间:2012-10-23 09:22:34

标签: c# return

C#的新手,我正在审查一些具有以下return语句的代码:

return (Object) null

这在C#中意味着什么,将返回什么?

由于

3 个答案:

答案 0 :(得分:11)

我能想到的唯一需要的地方是匿名方法,编译器无法推断返回类型。

例如,

var boxedThings = strings.Select(s =>
{
    int i;
    if (int.TryParse(s, out i))
        return i;
    double d;
    if (double.TryParse(s, out d))
        return d;
    return (object)null;
});
如果没有(object)

就无法编译。

答案 1 :(得分:6)

绝对等于return null

答案 2 :(得分:1)

您正在构建一个具有空值的Object。