【问题标题】:Nullable generic object collection可为空的通用对象集合
【发布时间】:2015-12-01 02:37:10
【问题描述】:
public class CubicMatrix<Object?>
    {
        private int width;
        private int height;
        private int depth;
        private Object[, ,] matrix;

        public CubicMatrix(int inWidth, int inHeight, int inDepth)
        {
            width = inWidth;
            height = inHeight;
            depth = inDepth;
            matrix = new Object[inWidth, inHeight, inDepth];
        }

        public void Add(Object toAdd, int x, int y, int z)
        {
            matrix[x, y, z] = toAdd;
        }

        public void Remove(int x, int y, int z)
        {
            matrix[x, y, z] = null;
        }

        public void Remove(Object toRemove)
        {
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int z = 0; z < depth; z++)
                    {
                        Object value = matrix[x, y, z];
                        bool match = value.Equals(toRemove);
                        if (match == false)
                        {
                            continue;
                        }

                        matrix[x, y, z] = null;
                    }
                }
            }
        }

        public IEnumerable<Object> Values
        {
            get
            {
                LinkedList<Object> allValues = new LinkedList<Object>();
                foreach (Object entry in matrix)
                {
                    allValues.AddLast(entry);
                }
                return allValues.AsEnumerable<Object>();
            }
        }

        public Object this[int x, int y, int z]
        {
            get
            {
                return matrix[x, y, z];
            }
        }

        public IEnumerable<Object> RangeInclusive(int x1, int x2, int y1, int y2, int z1, int z2)
        {
            LinkedList<Object> list = new LinkedList<object>();
            for (int a = x1; a <= x2; a++)
            {
                for (int b = y1; b <= y2; b++)
                {
                    for (int c = z1; c <= z2; c++)
                    {
                        Object toAdd = matrix[a, b, c];
                        list.AddLast(toAdd);
                    }
                }
            }

            return list.AsEnumerable<Object>();
        }

        public bool Available(int x, int y, int z)
        {
            Object toCheck = matrix[x, y, z];
            if (toCheck != null)
            {
                return false;
            }

            return true;
        }
    }

我在 C# 中创建了一个 Cubic Matrix 类来存储 3 维中的项目。我需要能够添加和删除项目,这就是我使用 Object 的原因? (我被引导理解您不能使用可为空的泛型,即 T?)。但是这种方法会产生错误

类型参数声明必须是标识符而不是类型

如果我不使用对象?虽然只是使用 Object 或 T 我得到了这个错误

无法将 null 转换为类型参数“T”,因为它可能是不可为空的值类型。考虑改用“default(T)”。

在这种情况下使用的正确语法和方法是什么?

【问题讨论】:

标签: c# object generics types nullable


【解决方案1】:

我想你想使用泛型参数T。您正在创建一个简单的容器类,因此允许任何泛型参数都是有意义的,无论它是否可以为空。要修复该错误,只需按照它所说的操作并使用default(T) 而不是null。

错误是因为T 可能是class 或struct,而structs 不能为空。因此,将T 类型的变量分配给null 是无效的。当T 是一个类时default(T) 是null,当T 是一个结构时是默认值。

【讨论】:

    【解决方案2】:

    当返回 default(T) 时(如您收到的错误所示),引用类型将返回 null,数字类型将返回 0,您的自定义类将返回 null,而 nullables 将返回 System.Nullable&lt;T&gt; .在 MSDN 上的default Keyword in Generic Code (C# Programming Guide) 中了解更多信息。

    【讨论】:

      【解决方案3】:

      如果您只想将泛型类型限制为对象 - 即没有结构或简单类型 - 您可以添加 where 子句

      public class CubicMatrix<T> where T : class
      

      这些意味着T只能是一个类。

      【讨论】:

      • 这不是真的,结构满足new() 约束。 Compiling example。您可能正在寻找 class 约束。
      • @31eee384 你是对的 - 谢谢,我已经更新了答案。
      • 使用对象约束会产生错误约束不能是特殊类'object' 但是使用 where T : 类约束有效。感谢您的帮助
      • @Darkreaper 谢谢 - 已更新。我的 C# 一定有点生疏了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多