Click here to Skip to main content
12,395,388 members (71,484 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.3K views
3 bookmarked
Posted

A usefull Dictionary extension

, 26 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple extension to map ditionary entries using lambda expressions

Introduction 

Initializing dictionaries is a pain in the neck. I've seen many clever ways to make it painless. Here goes  yet another one.  

This it's not my idea, I saw it a couple of days ago in some API but I can't remember where. This is my very simple implementation.  

The extension   

 
public static IDictionary<string, TValue> Map<TValue>(this IDictionary<string, TValue> 
	dictionary, Expression<Func<object, TValue>> pair)
{
	var key = pair.Parameters[0].Name;
	var eval = pair.Compile().Invoke(new object[1]);

	dictionary[key] = eval;

	return dictionary;
}   

Still we'll need some guard clauses and clean up. 

Using it 

var bag = new Dictionary<string, object>()
    .Map(Id => 1)
    .Map(Name => "Tom Bombadil")
    .Map(CreatedOn => DateTime.Today)
    .Map(CreatedBy => "root");

Assert.That(bag["Id"], Is.EqualTo(1));
Assert.That(bag["Name"], Is.EqualTo("Tom Bombadil"));
Assert.That(bag["CreatedOn"], Is.EqualTo(DateTime.Today));
Assert.That(bag["CreatedBy"], Is.EqualTo("root")); 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Erich Ledesma
Architect SunHotels
Spain Spain
I Received a Bachelor's Degree in Computer Science at the Mathematics and Computer Science Faculty, University of Havana, Cuba.

I mainly work in web applications using C# and some Javascript. Some very few times do some Java.

You may also be interested in...

Comments and Discussions

 
GeneralThat's an interesting technique Pin
Andreas Gieriet27-Mar-12 23:40
memberAndreas Gieriet27-Mar-12 23:40 
GeneralRe: That's an interesting technique Pin
Erich Ledesma27-Mar-12 23:51
memberErich Ledesma27-Mar-12 23:51 
QuestionWhy not just use a collection initializer? Pin
shiznit77026-Mar-12 11:50
membershiznit77026-Mar-12 11:50 
AnswerRe: Why not just use a collection initializer? Pin
Erich Ledesma26-Mar-12 19:58
memberErich Ledesma26-Mar-12 19:58 
SuggestionA small simplification and generification Pin
ekolis26-Mar-12 9:20
memberekolis26-Mar-12 9:20 
GeneralRe: A small simplification and generification Pin
Erich Ledesma26-Mar-12 10:26
memberErich Ledesma26-Mar-12 10:26 
GeneralRe: A small simplification and generification Pin
ekolis26-Mar-12 10:30
memberekolis26-Mar-12 10:30 
GeneralRe: A small simplification and generification Pin
Erich Ledesma26-Mar-12 20:20
memberErich Ledesma26-Mar-12 20:20 
GeneralRe: A small simplification and generification Pin
Sentenryu26-Aug-13 8:22
memberSentenryu26-Aug-13 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 26 Mar 2012
Article Copyright 2012 by Erich Ledesma
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid