1

Is there a function I can use to initialize Multiple List by using a given name List? For example:

List<string> nw_TCList = new List<string>();
List<string> ia_TCList = new List<string>();
List<string> st_TCList = new List<string>();
List<string> ud_TCList = new List<string>();
List<string> mb_TCList = new List<string>();

I would like to create thoese List, right now i have a List contains thoese name:

List<string> myNameList = new List<string>() 
{
    "nw_TCList", 
    "ia_TCList", 
    "st_TCList", 
    "ud_TCList", 
    "mb_TCList" 
};

I want to do something like this:

for (int i = 0; i < myNameList.Count(); i++)
{
   List<string> myNameList[i] = new List<string>();
}

Furthermore, Assume I have a dictionary myDic<sting,List<string>>, i have a Key list List<string> myKeys and many List<string> that i just created. I want to add them to the Dictionary something like this:

myDic.Add(myKeys[0], nw_TCList);
myDic.Add(myKeys[1], ia_TCList);
myDic.Add(myKeys[2], st_TCList);
myDic.Add(myKeys[3], ud_TCList);
myDic.Add(myKeys[4], mb_TCList);

Is there any easy way to do this job? Thanks a lot.

9
  • Do you need to create them before adding them to the dictionary? Commented Sep 6, 2018 at 8:55
  • 1
    Probably getting rid of all the xx_TCList variables in the first place is the way to go. Why do you have those? Commented Sep 6, 2018 at 8:56
  • you want their local variable names to assume their design-time name from a list of strings? are you after code generation, reflection, ... probably it is sufficient to add anonymous list instances to the dictionary and always access them by key. Commented Sep 6, 2018 at 8:56
  • @john before or after are both ok, i would like to know different ideas ; ) Commented Sep 6, 2018 at 9:02
  • 2
    It's weird that the variable name hold valuable information.. pretty sure it's some kind of X/Y Commented Sep 6, 2018 at 9:08

3 Answers 3

3

You could loop over the names and add an entry to the dictionary accordingly:

foreach(var name in new []{ "list", "names", "here" })
{
  myDic[name] = new List<string>();
}
Sign up to request clarification or add additional context in comments.

Comments

2

you can get rid of all the extra declared lists and collect them in the dictionary. If you have already all names in a list that you can use as the key, you can create a dictionary with empty lists and the corresponding keys using the ToDictionary method:

List<string> myNameList = new List<string>() 
{
    "nw_TCList", 
    "ia_TCList", 
    "st_TCList", 
    "ud_TCList", 
    "mb_TCList" 
};

Dictionary<string, List<string>> myDictionary = myNameList.ToDictionary
       (
           key => key, // this means that the current element in myNameList will serve as the key
           value => new List<string>() // this means that for the current element in myNameList a new List<string> will be created as the value
       );

Now you can access the lists the following way:

List<string> nw_TCList = myDictionary[myNameList[0]];

EDIT:

A little more robust approach would be to use an enum for the names. This way you could have the compiler to check for spelling. Declare an enum:

public enum ListNames
{
    nw_TCList,
    ia_TCList,
    st_TCList,
    ud_TCList,
    mb_TCList
}

An now use it as the Key in the dictionary. Now the construction of the dictionary looks a little different. You can get all values of the enum using the Enum.GetValue method.

Dictionary<ListNames, List<string>> myDictionaryRobust = Enum.GetValues(typeof(ListNames))
                             .Cast<ListNames>()
                             .ToDictionary(key => key, value => new List<string>());

Using this approach, the next time you think, that you need more of those lists, simply extend your enum, and the dictionary will be filled automatically with an extra list and the correct name!

1 Comment

Typo missing // . near value =>. With the 6 char edit rules i can't fix it ^^
0

It's same with foreach but in one line

var myDic = new Dictionary<string,List<string>>()
List<string> myKeys = new List<string>() { "1", "2", "3"};
myKeys.ForEach(l => myDic.Add(l, new List<string>));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.