Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading