<small id='5LNMb'></small><noframes id='5LNMb'>

      <tfoot id='5LNMb'></tfoot>
      • <bdo id='5LNMb'></bdo><ul id='5LNMb'></ul>

      1. <i id='5LNMb'><tr id='5LNMb'><dt id='5LNMb'><q id='5LNMb'><span id='5LNMb'><b id='5LNMb'><form id='5LNMb'><ins id='5LNMb'></ins><ul id='5LNMb'></ul><sub id='5LNMb'></sub></form><legend id='5LNMb'></legend><bdo id='5LNMb'><pre id='5LNMb'><center id='5LNMb'></center></pre></bdo></b><th id='5LNMb'></th></span></q></dt></tr></i><div id='5LNMb'><tfoot id='5LNMb'></tfoot><dl id='5LNMb'><fieldset id='5LNMb'></fieldset></dl></div>
        <legend id='5LNMb'><style id='5LNMb'><dir id='5LNMb'><q id='5LNMb'></q></dir></style></legend>

        条件运算符感到困惑,但为什么呢?

        时间:2023-10-06
      2. <small id='FpW43'></small><noframes id='FpW43'>

            <tbody id='FpW43'></tbody>

            <legend id='FpW43'><style id='FpW43'><dir id='FpW43'><q id='FpW43'></q></dir></style></legend>

                <i id='FpW43'><tr id='FpW43'><dt id='FpW43'><q id='FpW43'><span id='FpW43'><b id='FpW43'><form id='FpW43'><ins id='FpW43'></ins><ul id='FpW43'></ul><sub id='FpW43'></sub></form><legend id='FpW43'></legend><bdo id='FpW43'><pre id='FpW43'><center id='FpW43'></center></pre></bdo></b><th id='FpW43'></th></span></q></dt></tr></i><div id='FpW43'><tfoot id='FpW43'></tfoot><dl id='FpW43'><fieldset id='FpW43'></fieldset></dl></div>
                <tfoot id='FpW43'></tfoot>
                  <bdo id='FpW43'></bdo><ul id='FpW43'></ul>
                  本文介绍了条件运算符感到困惑,但为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设有两个类,都是同一个超类的后代,如下所示:

                  Assume two classes, both descendants of the same superclass, like this:

                  class MySuperClass{}
                  class A : MySuperClass{}
                  class B : MySuperClass{}
                  

                  那么这个赋值不会通过编译器:

                  Then this assignment won't pass the compiler:

                  MySuperClass p = myCondition ? new A() : new B();
                  

                  编译器抱怨 A 和 B 不兼容(无法确定条件表达式的类型,因为 'A' 和 'B' 之间没有隐式转换 [CS0173]).但它们都是 MySuperClass 类型,所以我认为这应该可行.并不是说这有什么大不了的;只需一个简单的转换就可以启发编译器.但它肯定是 C# 编译器中的一个障碍吗?你不同意吗?

                  The compiler complains that A and B are not compatible (Type of conditional expression cannot be determined because there is no implicit conversion between 'A' and 'B' [CS0173] ). But they are both of type MySuperClass, so in my opinion this should work. Not that it's a big deal; a simple cast is all it takes to enlighten the compiler. But surely it's a snag in the C# compiler? Don't you agree?

                  推荐答案

                  条件的结果应该是相同的类型.他们不是.

                  The results of the conditional should be of the same type. They are not.

                  来自 MSDN,(?:运算符):

                  From MSDN, (?: Operator):

                  first_expression 和 second_expression 的类型必须相同,或者必须存在从一种类型到另一种类型的隐式转换.

                  Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

                  由于 AB 不是同一类型,并且您似乎没有定义隐式转换,因此编译器会抱怨.

                  Since A and B are not the same type and you don't seem to have defined an implicit conversion, the compiler complains.

                  这篇关于条件运算符感到困惑,但为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:虚拟事件如何在 C# 中工作? 下一篇:如何使用 Mono 编译 Visual Studio C# 项目

                  相关文章

                        <bdo id='TkGS9'></bdo><ul id='TkGS9'></ul>
                      <i id='TkGS9'><tr id='TkGS9'><dt id='TkGS9'><q id='TkGS9'><span id='TkGS9'><b id='TkGS9'><form id='TkGS9'><ins id='TkGS9'></ins><ul id='TkGS9'></ul><sub id='TkGS9'></sub></form><legend id='TkGS9'></legend><bdo id='TkGS9'><pre id='TkGS9'><center id='TkGS9'></center></pre></bdo></b><th id='TkGS9'></th></span></q></dt></tr></i><div id='TkGS9'><tfoot id='TkGS9'></tfoot><dl id='TkGS9'><fieldset id='TkGS9'></fieldset></dl></div>
                      <legend id='TkGS9'><style id='TkGS9'><dir id='TkGS9'><q id='TkGS9'></q></dir></style></legend>

                    1. <tfoot id='TkGS9'></tfoot>

                      <small id='TkGS9'></small><noframes id='TkGS9'>