HOW TO POPULATE A LIST INSIDE A DICTIONARY?

ben reitman

Total Post:96

Points:676
Posted by  ben reitman
C# 
C#
 1697  View(s)
Ratings:
Rate this:

I have a dictionary that needs to be populate with a list. The code below does not work. How would I fix it?

clientinfo.cs

public Dictionary<int, List<string>> Languages { get; set; }

main.cs

var ClientsData = new List<MapModel.ClientInfo> { }

   ClientsData.Add(new MapModel.ClientInfo { Id = IDCounterDoctors, Languages = new Dictionary<int, List<string>>()});

   ClientsData[0].Languages.Add(2376, ["english", "french"]); // issue is here
  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to populate a list inside a dictionary?

    Hi Ben,


    Your initialization of list is wrong. Also make sure your languages property is initialized

    ClientsData[0].Languages = new Dictionary<int,List<string>>();

    ClientsData[0].Languages.Add(2376,new List<string>(){ "english", "french"});

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!