2015-09-04 101 views
1
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類來存儲三維中的項目。我需要能夠添加和刪除項目,這就是爲什麼我使用對象? (我已經明白你不能使用可空泛型,即T?)。但是這種方法gnerates錯誤可空對象通用對象集合

類型參數聲明必須是一個標識符不是一個類型

如果我不使用對象?雖然只是使用Object或T我得到這個錯誤,而不是

無法將null轉換爲類型參數'T',因爲它可能是一個不可爲空的值類型。考慮使用'default(T)'。

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

+0

這裏有一些可能的解決方案: http://stackoverflow.com/questions/19831157/c-sharp-generic-type-constraint-for-everything-nullable –

回答

1

我想你想使用通用參數T。你正在創建一個簡單的容器類,因此允許任何泛型參數都是有意義的,不管它是否可爲空。要解決這個錯誤,只需按照說明操作,並使用default(T)而不是null

錯誤是因爲T可能是classstruct,並且struct s不能爲空。因此,將類型爲T的變量分配給null是無效的。 default(T)nullT是一個類,默認值爲T是一個結構。

3

如果你想限制你的泛型類型只對象 - 即,無結構或簡單的類型 - 你可以添加where條款

public class CubicMatrix<T> where T : class 

這意味着T只能是一類。

+0

這是不正確,結構滿足['新()'](https://msdn.microsoft.com/en-us/library/sd2w2ew5.aspx)約束。 [編譯示例](http://ideone.com/hf56HY)。你可能在尋找'class'約束。 – 31eee384

+0

@ 31eee384你是對的 - 謝謝,我已經更新了答案。 –

+1

使用對象約束會生成錯誤_Constraint不能是特殊類'object'_ 但是使用where T:類約束起作用。感謝您的幫助 – Darkreaper