Home > Net >  How to insert a value at any position using a dictionary in C#
How to insert a value at any position using a dictionary in C#

Time:02-20

I have a car class and it has get and set methods. It includes both importing vehicle information and exporting that vehicle information. I use dictionary to store all vehicle information. I have created a car input function in the list class:

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

And now I want to insert at any position that I enter to put new car information into the dictionary, how will I do it?

Example:

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

CodePudding user response:

Try looking at an OrderedDictionary for this instead of a normal dictionary. OrderedDictionary.Insert will let you to specify the index at which the key will be inserted. Normal dictionaries use a key instead of an index, so this is not possible.

  •  Tags:  
  • c#
  • Related