在C#条件运算符结果类型 [英] Type result with conditional operator in C#

查看:148
本文介绍了在C#条件运算符结果类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用条件运算符,但我正在逐渐挂在它认为结果应该是类型。

I am trying to use the conditional operator, but I am getting hung up on the type it thinks the result should be.

下面是我做作,显示我有问题的例子:

Below is an example that I have contrived to show the issue I am having:

class Program
{
    public static void OutputDateTime(DateTime? datetime)
    {
        Console.WriteLine(datetime);
    }

    public static bool IsDateTimeHappy(DateTime datetime)
    {
        if (DateTime.Compare(datetime, DateTime.Parse("1/1")) == 0)
            return true;

        return false;
    }

    static void Main(string[] args)
    {
        DateTime myDateTime = DateTime.Now;
        OutputDateTime(IsDateTimeHappy(myDateTime) ? null : myDateTime);
        Console.ReadLine();                        ^
    }                                              |
}                                                  |
// This line has the compile issue  ---------------+

在该行上面指出的,我得到以下编译错误:

On the line indicated above, I get the following compile error:

无法确定的条件前pression的类型,因为之间有'&LT的隐式转换;空>和System.DateTime的

Type of conditional expression cannot be determined because there is no implicit conversion between '< null >' and 'System.DateTime'

我很困惑,因为该参数是可空类型(DateTime的?)。为什么它需要在所有的转换?如果为null,则使用的是,如果它是一个日期时间,然后使用它。

I am confused because the parameter is a nullable type (DateTime?). Why does it need to convert at all? If it is null then use that, if it is a date time then use that.

我是IM pression,根据:

I was under the impression that:

condition ? first_expression : second_expression;

的相同

if (condition)
   first_expression;
else
   second_expression;

显然这不是这种情况。这背后的理由?

Clearly this is not the case. What is the reasoning behind this?

(注意:?我知道,如果我做myDateTime一个不能为空的DateTime那么它会工作,但为什么它需要它

(NOTE: I know that if I make "myDateTime" a nullable DateTime then it will work. But why does it need it?

正如我前面所述,这是一个人为的例子。在我的真实的例子myDateTime是一个数据映射值不能可为空)。

As I stated earlier this is a contrived example. In my real example "myDateTime" is a data mapped value that cannot be made nullable.)

推荐答案

编译器并不推断结果的使用条件运算符的结果的类型,但是从类型它的参数。当它看到这前pression,因为它无法推断结果的类型的编译器失败:

The compiler does not infer the type of the result of the conditional operator from the usage of the result, but from the types of its arguments. The compiler fails when it sees this expression because it cannot deduce the type of the result:

IsDateTimeHappy(myDateTime) ? null : myDateTime;

由于的DateTime 不兼容,你需要告诉编译器的类型应该是什么。强制转换应该做的伎俩:

Since null and DateTime are not compatible, you need to tell the compiler what the type should be. A cast should do the trick:

DateTime? x = IsDateTimeHappy(myDateTime) ? (DateTime?)null : myDateTime;
OutputDateTime(x);

现在,编译器不会有任何问题。你也可以写上一行​​上,如果你preFER(但我可能不会做):

Now the compiler will have no problems. You can also write the above on one line if you prefer (but I would probably not do this):

OutputDateTime(IsDateTimeHappy(myDateTime) ? (DateTime?)null : myDateTime);

埃里克利珀有一个<一个href=\"http://stackoverflow.com/questions/1669492/are-there-any-good-reasons-why-ternaries-in-c-are-limited/1672282#1672282\">good回答这也是与此有关,并进入更多细节编译器如何确定类型。

Eric Lippert has a good answer that is also relevant here and goes into more details about how the compiler determines types.

这篇关于在C#条件运算符结果类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆