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

标签 c# compiler-construction

所以我遇到了一个有趣的编译器错误!我也将其粘贴在这里:“类型(我的类)必须是不可为 null 的类型才能用作泛型方法中的参数‘T’”

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

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? 中的 ?。如果 Item 是自定义类,默认情况下它可以为空。

关于c# - 类型(我的类)必须是不可空类型才能在泛型方法中用作参数 'T',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11713910/

相关文章:

从 gcc 的中间文件编译目标文件

javascript - 过滤、排序和分页方法的最快方法

c# - 使用 Response.BinaryWrite 和 Server.Mappath 将文件下载到特定位置可能吗?

c# - 替换嵌套的 foreach 以提高性能

objective-c - LLVM 在编译前会做简单的算术运算吗?

c++ - 在 c/c++ 中包括结构和编译

design-patterns - 窥孔优化模式

c# - 如何使用 XPath 和使用参数获取值?

c# - Azure功能:我无法选择消费计划

javascript - 在浏览器中实现自定义脚本类型?