It’s incredibly common to convert objects to dictionaries/maps and back, for all sorts of reasons. Jeff‘s co-worker was tasked with taking a dictionary which contained three keys, “mail”, “telephonenumber”, and “facsimiletelephonenumber” 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 across all of them to find the ones that we want. The dictionary in question is actually quite large, so we spend most of our time here looking at keys we don’t care about, to find the three we do. If only there were some easier way, some efficient option for finding items in a dictionary by their name. If only we could just fetch items by their key, then we wouldn’t need to have this loop. If only.

[Advertisement] Continuously monitor your servers for configuration changes, and report when there’s configuration drift. Get started with Otter today!

Remy Porter

Source link

You May Also Like

Disney Cracks Down On Copyright Infringement For People Picturing Mickey Mouse While Masturbating

BURBANK, CA— Threatening legal action against those using its intellectual property without…

We all miss unharmed funny prank like this, dont we?

submitted by /u/Careful_Cup4484 [comments] /u/Careful_Cup4484 Source link

‘Field of Dreams’ turns 35 and we’d be remiss if we didn’t highlight the cast then vs. now

Field of Dreams was released on April, 21st 1989. The film will…

FDA Rules Any White Liquid Can Be Called Milk

WASHINGTON—Announcing that the overly restrictive rules would be rolled back once and…