Skip to content
REPORTWIRE NEWSROOM
Breaking
Humor

They Key To Dictionaries

They Key To Dictionaries

[ad_1]

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!

[ad_2]

Remy Porter

Source link

ReportWire

ReportWire newsroom contributor.

Related Stories

Humor

We are plot armored.

[ad_1] We are plot armored. [ad_2] noreply@funnyjunk.com (beboba) Source link

Humor

Here we go

[ad_1] Tags: Gina valentina 2728 points, 142 comments. [ad_2] Source link