C# List<> Add lists to dictionary

后端 未结 2 477
有刺的猬
有刺的猬 2021-01-29 06:13
        Dictionary> SetPiese = new Dictionary>();

        char[] litere = \"ABCDEFGHIJLMNOPRSTUVXZ\".ToC         


        
2条回答
  •  一整个雨季
    2021-01-29 06:48

    Quite simply, don't declare them in separate variables to start with. That will always be a pain to work with programmatically. If you'd started with:

    List[] sets = new List[]
    {
        GenerareSetLitere("A", 1, 11),
        GenerareSetLitere("B", 9, 2),
        GenerareSetLitere("C", 1, 5)
        ...
    };
    

    then you could use:

    // Note loop condition change
    for (int i = 0; i < litere.Length; i++) {
        SetPiese.Add(litere[i], sets[i]);
    }
    

    Or even better, if literere is actually a bunch of expressions you can specify inline, you could do the whole thing in a collection initializer:

    Dictionary> SetPiese = new Dictionary>
    {
        { "first-key", GenerareSetLitere("A", 1, 11) },
        { "second-key", GenerareSetLitere("B", 9, 2) }
    };
    

    etc.

提交回复
热议问题