实现自己的KeyValuePair <tkey,tvalue =“”>版本的问题

时间:2018-09-26 06:50:13

标签: c#

在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

如何解决以上问题?

7 个答案:

答案 0 :(得分:3)

错误消息非常清楚:您的构造函数和ToString()缺少正文。您从KeyValuePair<,>中看到的代码只是一种合同,不是真实的代码。真实的代码可以在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}";
 }

答案 1 :(得分:1)

或者直接从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);
    }
}

答案 2 :(得分:1)

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

示例:

public ParameterNameValuePair(TParameterName parameterName, TValue value)
{
  ParameterName = parameterName;
  Value = value;
}

public override string ToString()
{
  return $"{ParameterName.ToString()} {Value.ToString()}";
}

答案 3 :(得分:0)

您需要实现这些方法。

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

答案 4 :(得分:0)

构造函数需要实现:

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; }
}

答案 5 :(得分:0)

此错误是由于缺少函数体(即构造函数)和重载的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()}";
            }
        }

答案 6 :(得分:0)

如果Visual Studio无法访问源,则它仅显示数据类型的“合同”。您可以找到KeyValuePair<TKey, TValue> 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}";
    }
}