C
C#2y ago
Boggo

❔ 2D Dictionary

i'm wondering if it would be possible to create a 2d Dictionary of sorts where it would look something like: Dictionary<key, Dictionary<key, value>>. Context is each server should have a dictionary of clients, with an associated tcpClient value.
3 Replies
rotor_
rotor_2y ago
The rule of thumb that I've always followed is that you should start to wrap your anonymous data into classes at this point, as it's the same thing but with more compiler hints Perhaps you should instead model your data as a Dictionary of Client classes, with a tcpClient property
record Client(TCPClient client, other properties ...);

var dict = new Dictionary<Client>{ ["client one"] = new Client(...) };

Console.WriteLine(dict["client one"].client);
record Client(TCPClient client, other properties ...);

var dict = new Dictionary<Client>{ ["client one"] = new Client(...) };

Console.WriteLine(dict["client one"].client);
It's pretty much the same logic as with dictionaries except you get the benefit of giving names to your properties, being able to store multiple properties and having better exceptions If you've got a whole collection of TCPClients per Client I'd still recommend storing that collection as a property - again to make the code more self-documenting
amio
amio2y ago
It might be much easier and clearer to just key your dictionary with a tuple. So not Dict<TKey1, Dict<TKey2, TValue>> but Dict<(TKey1, TKey2), TValue> But honestly that's more for quick stuff, Rotor is 100% right that you could and should make a "neater" model around this. Proper naming, type hierarchy that makes sense etc
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server