• Home
  • ASP.NET
  • JavaScript
  • SQL
  • WebService
  • OOPs
  • Jquery
  • GridView
  • MVC

Wednesday, February 10, 2016

Dictionary Initializers

 Unknown     9:31 PM     No comments   

Before C# 6, we could initialize a Dictionary using the { key, value } syntax:

1
2
3
4
5
var dictionary = new Dictionary<string, string>
{
     { "LAX", "Los Angeles International Airport" },
     { "MEL", "Melbourne Tullamarine Airport" }
};
While this syntax works fine, if you’re initializing complex objects with nested structures, all these curly braces get in the way and you may easily lose track of them.
C# 6 introduces a new index-like syntax to initialize dictionaries:
1
2
3
4
5
var dictionary = new Dictionary<string, string>
{
     ["LAX"] = "Los Angeles International Airport",
     ["MEL"] = "Melbourne Tullamarine Airport"
};
With this syntax, the extra noise due to curly braces is gone. Plus, keys stand out a bit more.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 comments:

Post a Comment

Popular Posts

Pages

  • Home
  • SQL
  • Asp-Net
  • GridView
  • Javascript
  • What are HttpHandlers and HttpModules in ASP.NET?

Blog Archive

  • March 2016 (12)
  • February 2016 (16)

Contact Form

Name

Email *

Message *

Followers

Follow us

  • Facebook
  • Twitter
  • Instagram
  • Pinterest
  • Bloglovin
  • Youtube

Copyright © Code Wrapper | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates