【问题标题】:Problem with implementation own version of KeyValuePair<TKey, TValue>实现自己的 KeyValuePair<TKey, TValue> 版本的问题
【发布时间】:2019-03-01 20:51:42
【问题描述】:

在 VS 2017 中,我们有一个 KeyValuePair 的实现,例如

    namespace System.Collections.Generic
    {
        //
        // Summary:
        //     Defines a key/value pair that can be set or retrieved.
        //
        // Type parameters:
        //   TKey:
        //     The type of the key.
        //
        //   TValue:
        //     The type of the value.
        public struct KeyValuePair<TKey, TValue>
        {
            //
            // Summary:
            //     Initializes a new instance of the System.Collections.Generic.KeyValuePair`2 structure
            //     with the specified key and value.
            //
            // Parameters:
            //   key:
            //     The object defined in each key/value pair.
            //
            //   value:
            //     The definition associated with key.
            public KeyValuePair(TKey key, TValue value);

            //
            // Summary:
            //     Gets the key in the key/value pair.
            //
            // Returns:
            //     A TKey that is the key of the System.Collections.Generic.KeyValuePair`2.
            public TKey Key { get; }
            //
            // Summary:
            //     Gets the value in the key/value pair.
            //
            // Returns:
            //     A TValue that is the value of the System.Collections.Generic.KeyValuePair`2.
            public TValue Value { get; }

            //
            // Summary:
            //     Returns a string representation of the System.Collections.Generic.KeyValuePair`2,
            //     using the string representations of the key and value.
            //
            // Returns:
            //     A string representation of the System.Collections.Generic.KeyValuePair`2, which
            //     includes the string representations of the key and value.
            public override string ToString();
        }
    }

我想用不同的名称实现我自己的 KeyValuePair。 我的代码

    namespace IEnumerableTest1
    {
        public struct ParameterNameValuePair<TParameterName, TValue>
        {
            public ParameterNameValuePair(TParameterName parameterName, TValue value);
            public TParameterName ParameterName { get; }
            public TValue Value { get; }
            public override string ToString();
        }
    }

现在,我的代码有错误:

错误 CS0501 'ParameterNameValuePair.ParameterNameValuePair(TParameterName, TValue)' 必须声明一个主体,因为它没有标记为抽象、外部或部分 IEnumerableTest1

错误 CS0501 'ParameterNameValuePair.ToString()' 必须声明一个主体,因为它没有标记为抽象、外部或部分 IEnumerableTest1

如何解决以上问题?

【问题讨论】:

  • 你需要实现构造函数ParameterNameValuePair甚至ToString函数。

标签: c#


【解决方案1】:

错误信息非常清楚:您的构造函数和ToString() 缺少主体。您从KeyValuePair&lt;,&gt; 显示的代码只是一种合同,而不是真正的代码。真实代码在reference source

你可以这样做:

 public struct ParameterNameValuePair<TParameterName, TValue>
 {
     // implement constructor to assign values
     public ParameterNameValuePair(TParameterName parameterName, TValue value)
     {
         ParameterName = parameterName;
         Value = value;
     }

     public TParameterName ParameterName { get; }
     public TValue Value { get; }

     // implement ToString() to return a meaningful string representation
     public override string ToString() => $"[{Key}]: {Value}";
 }

【讨论】:

    【解决方案2】:

    或直接来自Reference Source

    [Serializable]
    public struct KeyValuePair<TKey, TValue> {
        private TKey key;
        private TValue value;
    
        public KeyValuePair(TKey key, TValue value) {
            this.key = key;
            this.value = value;
        }
    
        public TKey Key {
            get { return key; }
        }
    
        public TValue Value {
            get { return value; }
        }
    
        public override string ToString() {
            StringBuilder s = StringBuilderCache.Acquire();
            s.Append('[');
            if( Key != null) {
                s.Append(Key.ToString());
            }
            s.Append(", ");
            if( Value != null) {
               s.Append(Value.ToString());
            }
            s.Append(']');
            return StringBuilderCache.GetStringAndRelease(s);
        }
    }
    

    【讨论】:

      【解决方案3】:

      这是因为您的 struct 构造函数和 ToString 方法没有主体。添加它们,然后它应该编译。

      例子:

      public ParameterNameValuePair(TParameterName parameterName, TValue value)
      {
        ParameterName = parameterName;
        Value = value;
      }
      
      public override string ToString()
      {
        return $"{ParameterName.ToString()} {Value.ToString()}";
      }
      

      【讨论】:

        【解决方案4】:

        你需要实现这些方法。

        当您转到程序集中代码的定义时,VS 显示的代码是以一种本身不是有效的 C# 的形式生成的:if 具有函数声明但没有实现。当您想知道一个类型的可用成员而不是如何编写代码时,这非常有用。

        【讨论】:

          【解决方案5】:

          构造函数需要实现:

          public struct ParameterNameValuePair<TParameterName, TValue>
          {
              public ParameterNameValuePair(TParameterName parameterName, TValue value)
              {
                  ParameterName = parameterName;
                  Value = value;
              }
              public TParameterName ParameterName { get; private set; }
              public TValue Value { get; private set; }
          }
          

          【讨论】:

            【解决方案6】:

            Visual Studio 仅在无法访问源时显示数据类型的协定。你可以找到KeyValuePair&lt;TKey, TValue&gt;here的实际实现。

            在您的具体示例中,您必须为每个方法提供一个实现,如下所示:

            public struct ParameterNameValuePair<TParameterName, TValue>
            {
                public ParameterNameValuePair(TParameterName parameterName, TValue value)
                {
                    ParameterName = parameterName;
                    Value = value;
                }
            
                public TParameterName ParameterName { get; }
                public TValue Value { get; }
                public override string ToString()
                {
                    return $"Parameter={ParameterName}, Value={Value}";
                }
            }
            

            【讨论】:

              【解决方案7】:

              此错误是由于缺少函数体,即构造函数和重载的 ToString() 实现你的构造函数。使用 get 和 set

              修改您的属性

              类似

              public struct ParameterNameValuePair<TParameterName, TValue>
                      {
                          private TParameterName  key;
                          private TValue value;
                          public ParameterNameValuePair(TParameterName parameterName, TValue value)
                          {
                               this.key = parameterName;
                               this.value= value;
                          }
              
                          public TParameterName ParameterName { get;}
                          public TValue Value { get;}
                          public override string ToString()
                          {
                               return $"Key: {ParameterName.ToString()}, Value:{Value.ToString()}";
                          }
                      }
              

              【讨论】:

              • 您的属性将无法按预期工作,它们不会访问支持字段。
              猜你喜欢
              • 2013-08-12
              • 1970-01-01
              • 1970-01-01
              • 2014-02-19
              • 2011-01-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多