我想寫一個通用的擴展方法,將一個固定矩陣添加到「彈性」矩陣。擴展方法編譯和(我認爲)它的代碼在常規方法中工作正常。知道我將使用這個功能很多各種類型,我更希望算出這個問題出來了,而不是用創可貼一起一瘸一拐:C#通用擴展方法編譯但不可用?
public void AddMatrix<T>(this List<T[]> MyList, T[,] Matrix)
{
if (MyList == null) throw new ArgumentNullException("MyList");
if (Matrix == null) throw new ArgumentNullException("Matrix");
for (int i = 0; i < Matrix.GetLength(0); i++)
{
T[] aLine = new T[Matrix.GetLength(1)];
for (int j = 0; j < Matrix.GetLength(1); j++)
aLine[j] = Matrix[i, j];
MyList.Add(aLine);
}
}
public void testAddMatrix()
{
List<string[]> aMyBigMatrix = new List<string[]>();
string[,] aSmallerMatrix =
{
{
"foo",
"bar",
"what"
}
};
aMyBigMatrix.AddMatrix(aSmallerMatrix); // .AddMatrix is not showing up here in Intellisense?
}
當你去使用它時會發生什麼? – 2012-04-13 18:22:15