使用变量作为类实例名C#

使用变量作为类实例名C#,c#,class,C#,Class,我在其他语言上找到了一些线索,但在C#上没有发现任何线索 基本上,我需要的是每次用不同的名称实例化一个类(以便能够在类之间创建链接) 我当前的代码: foreach (DataRow dataRow in RoutingObjects.Rows) { NodeViewModel node = CreateNode(dataRow.ItemArray[1].ToString(), new Point(globalx, globaly), false, dataRow.ItemArray[0

我在其他语言上找到了一些线索,但在C#上没有发现任何线索

基本上,我需要的是每次用不同的名称实例化一个类(以便能够在类之间创建链接)

我当前的代码:

foreach (DataRow dataRow in RoutingObjects.Rows)
{
    NodeViewModel node = CreateNode(dataRow.ItemArray[1].ToString(), new Point(globalx, globaly), false, dataRow.ItemArray[0].ToString());
}
这将为表中的每一行创建一个新的nodevewmodel。我真正需要的是每个nodevewmodel都有一个不同的名称,但我不知道如何做到这一点

我尝试在循环中使用一个简单的int,但是我不能在类实例名称中添加变量。代码(明显错误)如下所示:

int i = 0;
foreach (DataRow dataRow in RoutingObjects.Rows)
{
    NodeViewModel node+i = CreateNode(dataRow.ItemArray[1].ToString(), new Point(globalx, globaly), false, dataRow.ItemArray[0].ToString());
    i++;
}
我认为我应该以不同的方式做事,这可能是一个糟糕的想法,但我想得到一些建议!如果可能的话,我想知道——否则,一些替代方案将非常有用=)


谢谢

我想你在找一个收藏品。您可以使用
列表

蒂姆·施梅尔特:对。 当然,我们可以使用Linq获得更优雅的代码:

var nodelist = RoutingObjects.Rows.Select(dataRow=>CreateNode(dataRow.ItemArray[1].ToString(), new Point(globalx, globaly), false, dataRow.ItemArray[0].ToString())

不要使用
RoutingObjects.Rows.Select
你必须使用
RoutingObjects.AsEnumerable()。选择
因为a没有实现
IEnumerable
,而只实现
IEnumerable
。谢谢你,这正是我需要的!
NodeViewModel nvm = nodelist[0];
var nodelist = RoutingObjects.Rows.Select(dataRow=>CreateNode(dataRow.ItemArray[1].ToString(), new Point(globalx, globaly), false, dataRow.ItemArray[0].ToString())