C# 实现自己版本的KeyValuePair时出现问题<;TKey,TValue>;
在VS 2017中,我们实现了KeyValuePair,如C# 实现自己版本的KeyValuePair时出现问题<;TKey,TValue>;,c#,C#,在VS 2017中,我们实现了KeyValuePair,如 namespace System.Collections.Generic { // // Summary: // Defines a key/value pair that can be set or retrieved. // // Type parameters: // TKey: //
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();
}
}
namespace System.Collections.Generic
{
//
//总结:
//定义可以设置或检索的键/值对。
//
//类型参数:
//TKey:
//密钥的类型。
//
//t价值:
//值的类型。
公共结构KeyValuePair
{
//
//总结:
//初始化System.Collections.Generic.KeyValuePair`2结构的新实例
//使用指定的键和值。
//
//参数:
//关键:
//在每个键/值对中定义的对象。
//
//价值:
//与键关联的定义。
公钥值对(TKey key,TValue value);
//
//总结:
//获取键/值对中的键。
//
//返回:
//是System.Collections.Generic.KeyValuePair`2的密钥的TKey。
公钥{get;}
//
//总结:
//获取键/值对中的值。
//
//返回:
//一个TValue,它是System.Collections.Generic.KeyValuePair`2的值。
公共TValue值{get;}
//
//总结:
//返回System.Collections.Generic.KeyValuePair`2的字符串表示形式,
//使用键和值的字符串表示形式。
//
//返回:
//System.Collections.Generic.KeyValuePair`2的字符串表示形式,其中
//包括键和值的字符串表示形式。
公共重写字符串ToString();
}
}
我想用不同的名称实现我自己的KeyValuePair。
我的代码
名称空间IEnumerableTest1
{
公共结构参数NameValuePair
{
公共参数NameValuePair(TParameterName参数名称,TValue值);
公共TParameterName参数名{get;}
公共TValue值{get;}
公共重写字符串ToString();
}
}
现在,我在代码中出现了错误:
错误CS0501“ParameterNameValuePair.ParameterNameValuePair(TParameterName,TValue)”必须声明正文,因为它未标记为抽象、外部或部分IEnumerableTest1
错误CS0501“ParameterNameValuePair.ToString()”必须声明一个主体,因为它未标记为抽象、外部或部分IEnumerableTest1
如何解决上述问题?您需要实现这些方法
当您转到程序集中代码的定义时,VS显示的代码是以本身无效的形式生成的C#:if具有函数声明但没有实现。当您想知道类型的可用成员,但不想知道如何编写代码时,这非常有用。您需要实现这些方法
当您转到程序集中代码的定义时,VS显示的代码是以本身无效的形式生成的C#:if具有函数声明但没有实现。当您想知道类型的可用成员,但不想知道如何编写代码时,这非常有用。构造函数需要实现:
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; }
}
public结构参数NameValuePair
{
公共参数NameValuePair(TParameterName参数名称,TValue值)
{
ParameterName=ParameterName;
价值=价值;
}
公共TParameterName参数名称{get;private set;}
公共TValue值{get;private set;}
}
构造函数需要实现:
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; }
}
public结构参数NameValuePair
{
公共参数NameValuePair(TParameterName参数名称,TValue值)
{
ParameterName=ParameterName;
价值=价值;
}
公共TParameterName参数名称{get;private set;}
公共TValue值{get;private set;}
}
此错误是由于缺少函数体,即构造函数和重载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()}";
}
}
public结构参数NameValuePair
{
私有TParameterName密钥;
私人价值;
公共参数NameValuePair(TParameterName参数名称,TValue值)
{
this.key=参数名称;
这个。值=值;
}
公共TParameterName参数名{get;}
公共TValue值{get;}
公共重写字符串ToString()
{
返回$“键:{ParameterName.ToString()},值:{Value.ToString()}”;
}
}
此错误是由于缺少函数体,即构造函数和重载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()}";
}
}
public结构参数NameValuePair
{
私有TParameterName密钥;
私人价值;
公共参数NameValuePair(TParameterName参数名称,TValue值)
{
this.key=参数名称;
这个。值=值;
}
公共TParameterName参数名{get;}
公共TValue值{get;}
公共重写字符串ToString()
{
返回$“键:{ParameterName.ToString()},值:{Value.ToString()}”;
}
}
错误信息非常清楚:构造函数和ToString()
缺少实体。您在KeyValuePair
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}"; } }
[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);
}
}
public ParameterNameValuePair(TParameterName parameterName, TValue value)
{
ParameterName = parameterName;
Value = value;
}
public override string ToString()
{
return $"{ParameterName.ToString()} {Value.ToString()}";
}