【发布时间】: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#