【发布时间】:2013-03-16 19:16:37
【问题描述】:
有没有办法检索存储在列表中的对象的名称?
我想要做的是在打印出该矩阵的属性之前添加一个对象名称 - 在这种特殊情况下是矩阵的名称。
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());
一种写出特定对象名称的方法 - 矩阵。
【问题讨论】:
-
为什么不直接创建一个字符串名称属性并给它一个有意义的名称而不是 mat01、mat02 等?
-
感谢您的评论,但我不认为这是您暗示的问题的重复。从我的角度来看,变量名和对象名是不一样的,我不确定他们的方法是否可以应用于我的问题。
-
嗯,您可能想尝试一下链接。好像有办法。我会尝试一下(宝宝需要奶瓶:))
标签: c# list object identifier