例如我有一個通用類 KeyAndValue
public class KeyAndValue<T, U>
{
public T? key { get; set; }
public U? value { get; set; }
}
現在我想實體化一個LinkedListNode<KeyAndValue<int, int>>物件
var newNode = new LinkedListNode<KeyAndValue<int, int>>();
我收到這樣的警告
There is no argument given that corresponds to the required formal parameter 'value' of 'LinkedListNode<KeyAndValue<int, int>>.LinkedListNode(KeyAndValue<int, int>)' [146]",
我的問題是如何創建一個泛型型別的泛型物件?
uj5u.com熱心網友回復:
您可以使用現有的類KeyValuePair而不是KeyAndValue. 另外你可以創建一個建構式:
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; }
}
兩者都是可能的:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405258.html
標籤:
上一篇:dart中如何使用泛型。
下一篇:泛型回傳Nil為泛型型別
