[Python] C# equivalent of Python's defaultdict (for lists) in C# [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    What is the C# equivalent of doing:

    >>> from collections import defaultdict
    >>> dct = defaultdict(list)
    >>> dct['key1'].append('value1')
    >>> dct['key1'].append('value2')
    >>> dct
    defaultdict(<type 'list'>, {'key1': ['value1', 'value2']})


    For now, I have:

    Dictionary<string, List<string>> dct = new Dictionary<string, List<string>>();
    dct.Add("key1", "value1");
    dct.Add("key1", "value2");


    but that gives errors like "The best overloaded method match has invalid arguments".

    Continue reading...

Compartilhe esta Página