简体   繁体   中英

the type (my class) must be non-nullable type in order to use as a parameter 'T' in a generic method

So I am getting a fun compiler error! I'll paste it down here as well: "the type (my class) must be non-nullable type in order to use as a parameter 'T' in the generic method"

This doesn't make sense to me since my method is not generic. Here is how I am calling the offending code:

Item? inputtedItem = SearchProduct(txtProduct.Text);

Meanwhile, here is the definition of 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];
    }

I suppose I'll add the indexer from my inventory class in here for good measure:

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

Does anybody know what's wrong?

Thank you for the help.

I don't think you need the ? in Item? . If Item is a custom defined class, it will be nullable by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM