c# - 如何在 C# 中使用字典在任意位置插入值

标签 c#

我有一个汽车类,它有 get 和 set 方法。它包括导入车辆信息和导出车辆信息。我使用字典来存储所有车辆信息。我在列表类中创建了一个汽车输入函数:

Dictionary<string, Car> Dic = new Dictionary<string, Car>();
public void InputCar()
{
    Car car = new Car();
    car.Input();
    Dic.Add(car.BienSo1, car);
}

现在我想在输入的任意位置插入新车信息到字典中,我该怎么做?

示例:

Number Plate : 21112
Name Car : Honda
Number Plate : 33333
Name Car : Suzu
Insert at position 1 into the dictionary
Enter to put new car information
Number Plate : 21112
Name Car : Honda
Number Plate : 11111
Name Car : Toyota
Number Plate : 33333
Name Car : Suzuki

最佳答案

尝试查看 OrderedDictionary为此而不是普通的字典。 OrderedDictionary.Insert将允许您指定将插入键的索引。普通字典使用键而不是索引,因此这是不可能的。

关于c# - 如何在 C# 中使用字典在任意位置插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71193019/

相关文章:

c# - 类型转换为具有相同属性的另一个

c# - 复制不同类型对象的最简单方法

c# - EntityContainer 中未定义 EntitySet 'sproc_Forums_GetForumGroupByID'

c# - 从 ContinueWith 中抛出异常

c# - 将 OData 过滤器与 C# MVC 4 Web Api 和主干一起使用

c# - 如何只同步数据库中的某些行

c# - 如何在适用于 iOS 的 Xamarin Forms 中创建导航栏?

c# - 如何避免图像 MS Bot 构建器框架的自动重新调用

c# - 当我的计时器滴答作响时.......NET 内存泄漏

c# - 哪个 WPF 控件可以将网格作为子控件