They are key to dictionaries – The Daily WTF

It’s incredibly common to convert objects to dictionaries/maps and back, for a variety of reasons. JeffA coworker was tasked with entering a dictionary containing three keys, “mail,” “telephonenumber,” and “faximiletelephonenumber” into an object representing a contact. This was their solution:

foreach (string item in _ptAttributeDic.Keys)

string val = _ptAttributeDic[item];
switch (item)

    case "mail":
    if (string.IsNullOrEmpty(base._email))
        base._email = val;
    break;
    case "facsimiletelephonenumber":
    base._faxNum = val;
    break;
    case "telephonenumber":
    base._phoneNumber = val;
    break;


Yes, we iterate through all of them to find the ones we want. The dictionary in question is actually quite large, so we spend most of our time here looking for keys we don’t care about in order to find the three we do need. If only there was an easier way, some effective option to find dictionary items by name. If we could just retrieve items by their key, then we wouldn’t have this loop. Only if.

[Advertisement]

Continuously monitor your servers for configuration changes and report if configuration drift occurs. Get started with Otter today!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *