これで楽しいコンパイラエラーが発生します。私はここにも貼り付けます: "型(私のクラス)は、汎用メソッドでパラメータ 'T'として使用するためにnullableでない型でなければなりません"ジェネリックメソッドのパラメータ 'T'として使用するには、型(my class)をnull以外の型にする必要があります
これは私にとって意味がありません私の方法は一般的ではないからです。ここで私は問題のあるコードを呼び出しています方法です:
Item? inputtedItem = SearchProduct(txtProduct.Text);
はまた、ここでSearchProductの定義は次のとおりです。
private Item? SearchProduct(string product)
{
//If this is the first item to be entered into the inventory
if (_inventory == null || _inventory._productList.Count == 0)
{
return null;
}
//Return the Item's instance if it appears in the inventory. Otherwise return null.
return _inventory[product];
}
私は良い測定のために、ここで私の在庫クラスからインデクサを追加しますと仮定:
public Item this[string i]
{
get
{
Item returnItem;
_productList.TryGetValue(i, out returnItem);
return returnItem;
}
set
{
_productList.Add(i, value);
}
}
誰が間違っているのを知っていますか?
ありがとうございました。
'Item'は' struct'ですか? – dtb
'SearchProduct'はインターフェースメソッドの実装ですか? 'Item'を入力した汎用パラメータ' T'を持つインターフェースですか? – Virtlink
私はTryGetValueがあなたの一般的な方法だと思います。 _productListの型は正確に何ですか? –