type(我的类)必须是非可空类型才能在泛型方法中用作参数'T'

Nic*_*ick 5 c# compiler-construction

所以我得到了一个有趣的编译器错误!我也会在这里粘贴它:"类型(我的类)必须是非可空类型才能在泛型方法中用作参数'T'"

这对我来说没有意义,因为我的方法不通用.以下是我如何调用违规代码:

Item? inputtedItem = SearchProduct(txtProduct.Text);
Run Code Online (Sandbox Code Playgroud)

同时,这是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];
    }
Run Code Online (Sandbox Code Playgroud)

我想我会在这里添加我的库存类中的索引器以获得良好的衡量标准:

       public Item this[string i]
    {
        get
        {
            Item returnItem;
            _productList.TryGetValue(i, out returnItem);
            return returnItem;
        }
        set
        {
            _productList.Add(i, value);
        }
    }
Run Code Online (Sandbox Code Playgroud)

有人知道什么是错的吗?

感谢您的帮助.

The*_*son 6

我认为你不需要?in Item?.如果Item是自定义类,则默认情况下它可以为空.