C# 如何在程序运行时创建列表

C# 如何在程序运行时创建列表,c#,.net,list,C#,.net,List,当我的程序运行时,它会在一条消息中接收带有Id和数据的消息 我想为每个Id创建一个新的列表,在那里我可以存储来自该Id的数据。 问题是,在我的程序运行之前,我不知道我将收到多少Id。我唯一知道的是,这是很多。所以我不知道这是否可能,或者我应该怎么做。 这是我基本上想做的: if (!(idlist.Contains(id))){ idlist.Add(id); List<string> id.ToString() = new List<string>();} if

当我的程序运行时,它会在一条消息中接收带有Id和数据的消息

我想为每个Id创建一个新的列表,在那里我可以存储来自该Id的数据。 问题是,在我的程序运行之前,我不知道我将收到多少Id。我唯一知道的是,这是很多。所以我不知道这是否可能,或者我应该怎么做。 这是我基本上想做的:

if (!(idlist.Contains(id))){

 idlist.Add(id);
 List<string> id.ToString() = new List<string>();}
if(!(idlist.Contains(id))){
idlist.Add(id);
列表id.ToString()=新列表();}

您可以执行以下操作:

Dictionary<int, List<string>> dictionary = new Dictionary<int, List<string>>();
dictionary[newId] = new List<string>();
dictionary[newId].add("Hello!");
Dictionary Dictionary=newdictionary();
字典[newId]=新列表();
字典[newId]。添加(“你好!”);
字典很方便

您也可以这样做:

if(!dictionary.ContainsKey(newId)){
    //Add the new List<string> to the dictionary
}else{
    //Add to the existing List<string> at dictionary[newId]
}
if(!dictionary.ContainsKey(newId)){
//将新列表添加到字典中
}否则{
//添加到字典[newId]中的现有列表
}

希望这有帮助

您可以执行以下操作:

Dictionary<int, List<string>> dictionary = new Dictionary<int, List<string>>();
dictionary[newId] = new List<string>();
dictionary[newId].add("Hello!");
Dictionary Dictionary=newdictionary();
字典[newId]=新列表();
字典[newId]。添加(“你好!”);
字典很方便

您也可以这样做:

if(!dictionary.ContainsKey(newId)){
    //Add the new List<string> to the dictionary
}else{
    //Add to the existing List<string> at dictionary[newId]
}
if(!dictionary.ContainsKey(newId)){
//将新列表添加到字典中
}否则{
//添加到字典[newId]中的现有列表
}
希望这有帮助

您可以使用它来存储键值对列表。您的键是id,值是字符串列表

Dictionary<int,List<string>> ids = new Dictionary<int,List<string>>();
Dictionary id=newdictionary();
当您获得一个ID时,您会创建一个新条目,如下所示:

ids[id] = new List<string>();
IDictionary<int, List<string>> idDictionary = new Dictionary<int, List<string>>()
//wait for messages
...
//handle new message
if (!idDictionary.containsKey(incommingId)){
     idDictionary.add(incommingId, new List<String>())
}
idDictionary[incommingId].add(data);
ids[id]=newlist();
如果字典中已包含此ID的条目,则它将被覆盖。您可以使用检查来防止:

if(!ids.ContainsKey(id))
{
    ids[id] = new List<string>();
}
if(!ids.ContainsKey(id))
{
ids[id]=新列表();
}
您可以使用它来存储键值对列表。您的键是id,值是字符串列表

Dictionary<int,List<string>> ids = new Dictionary<int,List<string>>();
Dictionary id=newdictionary();
当您获得一个ID时,您会创建一个新条目,如下所示:

ids[id] = new List<string>();
IDictionary<int, List<string>> idDictionary = new Dictionary<int, List<string>>()
//wait for messages
...
//handle new message
if (!idDictionary.containsKey(incommingId)){
     idDictionary.add(incommingId, new List<String>())
}
idDictionary[incommingId].add(data);
ids[id]=newlist();
如果字典中已包含此ID的条目,则它将被覆盖。您可以使用检查来防止:

if(!ids.ContainsKey(id))
{
    ids[id] = new List<string>();
}
if(!ids.ContainsKey(id))
{
ids[id]=新列表();
}
使用字典:

var myDictionary = new Dictionary<int, List<string>>();
// .....
List<string> myList;
myDictionary.TryGetValue( id, out myList );
if ( null == myList ) {
    myList = new List<string>();
    myDictionary[id] = myList;
}
myList.Add( "hello world" );
var myDictionary=newdictionary();
// .....
列出我的清单;
TryGetValue(id,out myList);
if(null==myList){
myList=新列表();
myDictionary[id]=myList;
}
myList.Add(“hello world”);
使用字典:

var myDictionary = new Dictionary<int, List<string>>();
// .....
List<string> myList;
myDictionary.TryGetValue( id, out myList );
if ( null == myList ) {
    myList = new List<string>();
    myDictionary[id] = myList;
}
myList.Add( "hello world" );
var myDictionary=newdictionary();
// .....
列出我的清单;
TryGetValue(id,out myList);
if(null==myList){
myList=新列表();
myDictionary[id]=myList;
}
myList.Add(“hello world”);

我不相信你能用你正在尝试的方式创建一个列表。您可能想尝试以下内容:

ids[id] = new List<string>();
IDictionary<int, List<string>> idDictionary = new Dictionary<int, List<string>>()
//wait for messages
...
//handle new message
if (!idDictionary.containsKey(incommingId)){
     idDictionary.add(incommingId, new List<String>())
}
idDictionary[incommingId].add(data);
IDictionary idDictionary=new Dictionary()
//等待消息
...
//处理新消息
if(!idDictionary.containsKey(incommingId)){
idDictionary.add(incommingId,new List())
}
idDictionary[incommingId]。添加(数据);

使用字典保存所有接收到的id的数据列表应该可以提供良好的查找性能。但是,如果您在程序运行时收到数百个不同的id,这可能会发生变化。

我不相信您能够以尝试的方式创建列表。您可能想尝试以下内容:

ids[id] = new List<string>();
IDictionary<int, List<string>> idDictionary = new Dictionary<int, List<string>>()
//wait for messages
...
//handle new message
if (!idDictionary.containsKey(incommingId)){
     idDictionary.add(incommingId, new List<String>())
}
idDictionary[incommingId].add(data);
IDictionary idDictionary=new Dictionary()
//等待消息
...
//处理新消息
if(!idDictionary.containsKey(incommingId)){
idDictionary.add(incommingId,new List())
}
idDictionary[incommingId]。添加(数据);

使用字典保存所有接收到的id的数据列表应该可以提供良好的查找性能。但是,如果在程序运行时收到数百个不同的id,这可能会发生变化。

为什么不在
if
语句中使用
TryGetValue()
返回的布尔值?因为“MyDictionary”对于指定的键可以有值“NULL”。我希望其中没有空列表。事实上,在这种情况下,返回值是否无用。为什么不在
if
语句中使用
TryGetValue()
返回的布尔值呢?因为“MyDictionary”可以为指定的键指定值“NULL”。我希望其中没有空列表。事实上,在这种情况下,返回值是无用的。