Search code examples
c#listhashable

How to address specific element in list


I want to create "list of list of list". It should be:

Group (has a list of Members)

Member (has a Name and list of Properties)

Property (has Name and Value)

What I want is to have a possibility to add Property into Member (specified by its name) inside defined Group. Someting like this:

membersgroup.AddNewMember(memberXYZ);
...
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ).

I have trouble achieving this using list... I found class Hashable, but I am not sure if this is usable... and cannot make it works too...

Thank for any suggestion :)


Solution

  • public class Group
    {
    public Group()
    {
        Members = new List<Member>();
    }
    
    public IEnumerable<Member> Members { get; set; }
    }
    
    public class Member
    {
    public Member()
    {
        Properties = new Dictionary<string, string>();
    }
    
    public string Name { get; set; }
    IDictionary<string, string> Properties { get; set; }
    }
    

    The dictionary can take a key and a value, and the key should be unique. You can also create a class property if you want to add another thing beside the name and the value