[英]How to instantiate a linkedlistNode of a generic type in c#
For example I have a generic class KeyAndValue例如我有一个通用的 class KeyAndValue
public class KeyAndValue<T, U>
{
public T? key { get; set; }
public U? value { get; set; }
}
Now I want to instantiate a LinkedListNode<KeyAndValue<int, int>>
object现在我想实例化一个LinkedListNode<KeyAndValue<int, int>>
object
var newNode = new LinkedListNode<KeyAndValue<int, int>>();
I received warning like this我收到这样的警告
There is no argument given that corresponds to the required formal parameter 'value' of 'LinkedListNode<KeyAndValue<int, int>>.LinkedListNode(KeyAndValue<int, int>)' [146]",
My question is how to create an object that is generic type of generic type?我的问题是如何创建一个泛型类型的泛型类型的 object?
You can use the existing class KeyValuePair
instead of KeyAndValue
.您可以使用现有的 class KeyValuePair
代替KeyAndValue
。 Additional you can create a constructor:另外你可以创建一个构造函数:
public class KeyAndValue<T, U>
{
public KeyAndValue(T key, U value)
{
this.key = key;
this.value = value;
}
public T? key { get; set; }
public U? value { get; set; }
}
Both is possible:两者都是可能的:
var keyValuePair = new KeyValuePair<int, int>(0, 5);
var linkedListNode1 = new LinkedListNode<KeyValuePair<int, int>>(keyValuePair);
var keyAndValue = new KeyAndValue<int, int>(0, 5);
var linkedListNode2 = new LinkedListNode<KeyAndValue<int, int>>(keyAndValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.