DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Solving a Coding Mystery

Everyone likes a good puzzle — unless it's in production, which is what happened here. Can you figure out what the issue is?

Oren Eini user avatar by
Oren Eini
·
Jan. 24, 17 · Opinion
Like (0)
Save
Tweet
Share
2.96K Views

Join the DZone community and get the full member experience.

Join For Free

This was a surprising shock. This code seems so simple, but it does something very different than what I would expect.

var doc = new Dictionary<string,object>
{
    ["@metadata"] = new Dictionary<string, object>
    {
        ["@id"] = "users/1"
    }
    ["Name"] = "Oren"
};

Console.WriteLine(doc["Name"]);

The question is: Why?

As it turns out, we are missing one character here:

image

Notice the lack of the comma? Let's see how the compiler is treating this code by breaking it apart into steps, shall we?

First, let's break it into two statements:

var item = new Dictionary<string, object>
    {
        ["@id"] = "users/1"
    }
    ["Name"] = "Oren";
var doc = new Dictionary<string,object>
{
    ["@metadata"] = item
};

Note that we moved the name line into the first statement since there isn’t a command here. However, what is it actually doing? This looks very strange, but that is just because we have the dictionary initializer here. If we drop it, we get:

image

That makes a lot more sense. If we break it all down, we have:

var tmp1 = new Dictionary<string, object>();

tmp1["@id"] = "users/1";
tmp1["Name"] = "Oren";

var tmp2 = tmp1["Name"];

var tmp3 = new Dictionary<string, object>();

tmp3["@metadata"] = tmp2;

var doc = tmp3;

Console.WriteLine(doc["Name"]);

That explains it all. It makes perfect sense — and it's a very nasty trap. We ran into it accidentally in production code, and it was near impossible to figure out what was going on or why it happened.

Coding (social sciences) Drops (app) Dictionary (software) Production (computer science) IT Command (computing)

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fixing Bottlenecks in Your Microservices App Flows
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • A Guide to Understanding XDR Security Systems
  • 10 Things to Know When Using SHACL With GraphDB

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: