Friday, September 11, 2009

C# Puzzle No.19 (intermediate)

Suppose we have a mapping between two sets of labels.

Dictionary<string, string> mapping = new Dictionary<string, string>();
 
 mapping.Add( "label1", "description of label1" );
 mapping.Add( "label2", "description of label2" );
 mapping.Add( "label3", "description of label3" );
 mapping.Add( "label4", "description of label4" );

If we get a label from the set of keys, we have to change it into its counterpart. For example "label1" should become "description of label1" etc.


If we get any other label, we leave it with no changes. So "just a label" should remain as "just a label".


Your goal is to write a single LINQ expression that does the job. Specifically



string test1 = "label1";
string test2 = "just a label";
 
  Console.WriteLine(
      mapping
          linq...expression...for...test1...here
          );
 
  Console.WriteLine(
      mapping
          linq...expression...for...test2...here
          );

should print



description of label1
just a label
on the console.