簡體   English   中英

可為空的通用對象集合

[英]Nullable generic object collection

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”,因為它可能是不可為 null 的值類型。 考慮使用 'default(T)' 代替。

在這種情況下使用的正確語法和方法是什么?

如果您想將泛型類型限制為僅對象 - 即沒有結構或簡單類型 - 您可以添加where子句

public class CubicMatrix<T> where T : class

這意味着T只能是一個類。

我認為您想使用通用參數T 您正在創建一個簡單的容器類,因此允許任何泛型參數都是有意義的,無論它是否可以為空。 要修復錯誤,只需按照它說的做並使用default(T)而不是null

錯誤是因為T可能是一個class或一個struct ,並且struct s 不能為空。 因此,將類型為T的變量賦值為null是無效的。 T是一個類時default(T)null ,當T是一個結構時默認值。

當返回default(T) (如您所提示的錯誤),引用類型將返回null ,數字類型將返回0 ,您的自定義類將返回null並且可空對象將返回System.Nullable<T> MSDN 上通用代碼(C# 編程指南)中的默認關鍵字中有更多關於此的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM