• <tfoot id='l2Jay'></tfoot>

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

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

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

      1. 操作员 '?'不能应用于“T"类型的操作数

        时间:2023-11-10
        <i id='u8qMl'><tr id='u8qMl'><dt id='u8qMl'><q id='u8qMl'><span id='u8qMl'><b id='u8qMl'><form id='u8qMl'><ins id='u8qMl'></ins><ul id='u8qMl'></ul><sub id='u8qMl'></sub></form><legend id='u8qMl'></legend><bdo id='u8qMl'><pre id='u8qMl'><center id='u8qMl'></center></pre></bdo></b><th id='u8qMl'></th></span></q></dt></tr></i><div id='u8qMl'><tfoot id='u8qMl'></tfoot><dl id='u8qMl'><fieldset id='u8qMl'></fieldset></dl></div>

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

            <tfoot id='u8qMl'></tfoot>
                <legend id='u8qMl'><style id='u8qMl'><dir id='u8qMl'><q id='u8qMl'></q></dir></style></legend>

                  <tbody id='u8qMl'></tbody>
                  <bdo id='u8qMl'></bdo><ul id='u8qMl'></ul>
                • 本文介绍了操作员 '?'不能应用于“T"类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  试图使 Feature 泛型然后突然编译器说

                  Trying to make Feature generic and then suddenly compiler said

                  运算符'?'不能应用于T"类型的操作数

                  这里是代码

                  public abstract class Feature<T>
                  {
                      public T Value
                      {
                          get { return GetValue?.Invoke(); } // here is error
                          set { SetValue?.Invoke(value); }
                      }
                  
                      public Func<T> GetValue { get; set; }
                      public Action<T> SetValue { get; set; }
                  }
                  

                  可以改用这段代码

                  get
                  {
                      if (GetValue != null)
                          return GetValue();
                      return default(T);
                  }
                  

                  但我想知道如何修复那个漂亮的 C# 6.0 单线.

                  But I am wondering how to fix that nice C# 6.0 one-liner.

                  推荐答案

                  由于不是所有的东西都可以为 null,你必须缩小 T 为可以为空的东西(又名对象).结构不能为空,枚举也不能.

                  Since not everything can be null, you have to narrow down T to be something nullable (aka an object). Structs can't be null, and neither can enums.

                  class 上添加 where 确实可以解决问题:

                  Adding a where on class does fix the issue:

                  public abstract class Feature<T> where T : class
                  

                  那么为什么它不能正常工作呢?

                  So why doesn't it just work?

                  Invoke() 产生 T.如果 GetValuenull,则 ? 运算符将 T 类型的返回值设置为 null,它不能.例如,如果 Tint,它不能使其为空 (int?),因为需要的实际类型 (T = int) 不是.

                  Invoke() yields T. If GetValue is null, the ? operator sets the return value of type T to null, which it can't. If T is int for example, it can't make it nullable (int?) since the actual type required (T = int) isn't.

                  如果你把代码中的T改为int,你就会很清楚的看到问题所在.你问的最终结果是这样的:

                  If you change T to be int in your code, you will see the problem very clearly. The end result of what you ask is this:

                  get
                  {
                      int? x = GetValue?.Invoke();
                      return x.GetValueOrDefault(0);
                  }
                  

                  这不是空传播运算符将为您做的事情.如果您恢复使用 default(T) 它确实知道该做什么,并且您避免了有问题的"空传播.

                  This is not something the null-propagation operator will do for you. If you revert to the use of default(T) it does know exactly what to do and you avoid the 'problematic' null-propagation.

                  这篇关于操作员 '?'不能应用于“T"类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                      <bdo id='cYYu1'></bdo><ul id='cYYu1'></ul>

                          <tbody id='cYYu1'></tbody>

                        • <small id='cYYu1'></small><noframes id='cYYu1'>