2011-12-06 39 views
1

我已經從名單列表繼承的類上newing它實現一個索引應用索引從集合C#繼承

public class TwoDList<T>: List<List<T>> 
{ 
    public T this[int row, int column] 
     { 
      get; 
      set ; 
     } 
} 

,並使用它像這樣:

TwoDCollection<int> target = new TwoDCollection<int>(); 
    var linearSequecValue = target[0, 2]; 

但我得到一個編譯時錯誤「沒有超載的方法」這個'需要2個參數「

+0

TwoDList和TwoDCollection?錯字? –

回答

2

它工作正常(假設您提供getset機構);是這樣的:

  • TwoDList<T>/TwoDCollection<T>之間的錯字?
  • 你真的在List<T>/IList<T>的某處打字嗎?

我也應該說:這通常是List<T>一個壞主意繼承提供的功能; 封裝它會更好。

工作例如:

class Program 
{  
    static void Main() 
    { 
     TwoDList<int> target = new TwoDList<int>(); 
     var linearSequecValue = target[0, 2]; 
    } 
} 

public class TwoDList<T> : List<List<T>> 
{ 
    public T this[int row, int column] 
    { 
     get { return this[row][column]; } 
     set { this[row][column] = value; } 
    } 
} 
1

我認爲你應該使用:

TwoDList<int> target = new TwoDList<int>(); 
var linearSequecValue = target[0, 2]; 

這是我的嘗試:

public int Test() 
    { 
     TwoDList<int> target = new TwoDList<int>(); 
     target.Add(new List<int>(new int[] {3,5,6})); 
     target.Add(new List<int>(new int[] {2,1,8})); 
     target.Add(new List<int>(new int[] {1,3,4})); 
     return target[1, 2]; 
    } 

public class TwoDList<T> : List<List<T>> 
{ 
    public T this[int row, int column] 
    { 
     get { return this[row][column]; } 
     set { this[row][column] = value; } 
    } 
} 
0

試試這個

TwoDList<int> target = new TwoDList<int>(); 
    var linearSequecValue = target[0, 2]; 

你可以試試這個

public class TwoDList<T> : List<List<T>> 
{ 
    public T this[int row, int column] 
    { 
     get { return this[row][column]; } 
     set { this[row][column] = value; } 
    } 
} 

,而不是這個

public class TwoDList<T>: List<List<T>> 
{ 
    public T this[int row, int column] 
     { 
      get;  
      set ; 
     } 
} 
0
public class TwoDList<T> : List<List<T>> 
{ 
    public T this[int row, int column] 
    { 
     get { return (this[row])[column]; } 
    } 
} 

TwoDList<int> target = new TwoDList<int>(); 
var linearSequecValue = target[0, 2]; 

工作正常

什麼TwoDCollection的定義是什麼?

0

你有你的indexerderived type但應該有一個custom implementation in your indexer body(對,get; set;,因爲你已經繼承了2 Lists<>

現在,無需更改代碼,你可以use like this

var linearSequecValue = target[0][2]; 

希望這有助於!