What's nice about jQuery, which is a great JavaScript library, is to be able to get the element you are working on as return value. Here is an example of what I am referring to:
$(function() {
$("#poo").css("clear", "both").css("margin", "10px");
});
I would like to implement the same for the C# IDictionary
interface
so that I can write code as follows:
IDictionary<string, string> myDictionary =
new Dictionary<string, string>().
Add("key1", "value1").
Add("key2", "value2").
Add("key3", "value3");
So I have created an extension method as below for IDictionary
:
public static IDictionary<string, string> AddAndReturn(this IDictionary<string, string> dictionary, string key, string value) {
dictionary.Add(key, value);
return dictionary;
}
And now I am able to use it like:
IDictionary<string, string> poo =
new Dictionary<string,string>().
AddAndReturn("key1", "value1").
AddAndReturn("key2", "value2").
AddAndReturn("key3", "value3");
I wonder if I am following the right path here.
Is what I am doing a poor man's fluent interface implementation or totally unrelated to that?
Is there any known case for this implementation in C#?