有没有办法检索存储在列表中的对象的名称?
我想要做的是在打印出该矩阵的属性之前添加一个对象名称 - 在这种特殊情况下是矩阵的名称。
internal class Program
{
private static void Main(string[] args)
{
//Create a collection to help iterate later
List<Matrix> list_matrix = new List<Matrix>();
//Test if all overloads work as they should.
Matrix mat01 = new Matrix();
list_matrix.Add(mat01);
Matrix mat02 = new Matrix(3);
list_matrix.Add(mat02);
Matrix mat03 = new Matrix(2, 3);
list_matrix.Add(mat03);
Matrix mat04 = new Matrix(new[,] { { 1, 1, 3, }, { 4, 5, 6 } });
list_matrix.Add(mat04);
//Test if all methods work as they should.
foreach (Matrix mat in list_matrix)
{
//Invoking counter of rows & columns
//HERE IS what I need - instead of XXXX there should be mat01, mat02...
Console.WriteLine("Matrix XXXX has {0} Rows and {1} Columns", mat.countRows(), mat.countColumns());
}
}
}
总之我需要这里
Console.WriteLine("Matrix XXXX has {0} Rows and {1} Columns",
mat.countRows(),
mat.countColumns());
一种写出特定对象名称的方法 - 矩阵。