Given this code example:-
public readonly Dictionary
public void Bind
{
providers[typeof(T)] = () => instance;
}
My question is why is a Lambda expression used for the assignment of the parameter "instance" to providers[typeof(T)]?
I tried assigning the "instance" parameter on its own but got a cast\conversion error.
Regards
Steven

VulpesRank #55 · 29.2K reputationPosted Sep 10, 2012, 9:47 AM EDT
string instance = "Steven";
So, we're invoking the delegate func, using the usual C# syntax (i.e. func()) given that it doesn't take an argument. Instead the lambda expression which we're assigning to func is 'capturing' the local variable, instance and returning that.
The Dictionary is associating a type object for a given type with a delegate which returns a value of type object.
The Bind() method ensures that the actual type of the bound value is the same as the type object because both of them have to be of type T.
VulpesRank #55 · 29.2K reputationPosted Sep 9, 2012, 2:06 PM EDT
() => instance
fits the bill because:
Guest UserRank #4,868 · 321 reputationPosted Sep 10, 2012, 7:51 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 10, 2012, 8:10 AM EDT
The Dictionary's value is a delegate (i.e. the lambda expression) which doesn't get executed until it's actually invoked.
Here's a quick example I've put together to show how you might use the Bind method:
Guest UserRank #4,868 · 321 reputationPosted Sep 10, 2012, 8:29 AM EDT
Sorry for being dense but how is:-
t.providers[typeof(string)]();
invoking the lambda expression?
as all that is being passed is the Key which is the Type? I thought type parameters and formal parameters work in conjunction with each other ie Key and Value, or is it that the Bind method is handling this with the assignment of the Lambda?
Also am I right in saying the Dictionary is just a surrogate in effect for resolving the Type and Instance rather than having anything actually added to it?
Regards
Guest UserRank #4,868 · 321 reputationPosted Sep 10, 2012, 9:57 AM EDT
Guest UserRank #4,868 · 321 reputationPosted Sep 11, 2012, 8:29 AM EDT
Apologies for revisiting
Going back on a statement you said as follows:-
"t.providers[typeof(string)] is analogous to func in the above example because it's a delegate of type Func
I thought providers[typeof(string)] was the "Key" part of the Dictionary of type Type and the "Value" part of the Dictionary a Delegate of type Func
also, how is Func
VulpesRank #55 · 29.2K reputationPosted Sep 11, 2012, 10:43 AM EDT
As the above value is a delegate of type Func
Guest UserRank #4,868 · 321 reputationPosted Sep 11, 2012, 12:31 PM EDT
With regards to "typeof(string) is the key and t.providers[typeof(string)] is the value corresponding to that key."
I dont see how these two are any different? id expect () => instance to be the Func
Sorry for my lack of understanding...
VulpesRank #55 · 29.2K reputationPosted Sep 11, 2012, 2:44 PM EDT
You're also correct that () => instance is the Func
Guest UserRank #4,868 · 321 reputationPosted Sep 12, 2012, 12:05 PM EDT
Given your statement:-
object obj = t.providers[typeof(string)]();
" By doing this we're invoking the lambda expression () => instance, which returns a value of type object".
How is the Lambda expression being invoked, is it because of the parenthesis () which is invoking the Func
Also, I tried the following
providers[typeof(T)](() => instance);
However I get an error that "Func does not have 1 arguement" so is this syntax only applicable if Func has an input parameter declared? otherwise, the use of the assignment operator = is required for no arguements?
Regards
Steven
VulpesRank #55 · 29.2K reputationPosted Sep 12, 2012, 2:20 PM EDT
Guest UserRank #4,868 · 321 reputationPosted Sep 12, 2012, 10:54 PM EDT
string name = "somename";
t.Bind(name);
var getResult = t.providers[typeof(String)]();
// Execute the Delegate Func
Regards
VulpesRank #55 · 29.2K reputationPosted Sep 13, 2012, 1:01 AM EDT
So the lambda expression (and the captured parameter) continue to exist even though the Bind method which created them has terminated execution.
This is achieved behind the scenes by the compiler creating a hidden class and making the captured variable a field of that class. When the delegate is invoked, the class is instantiated and bound to the delegate. Consequently, the lambda expression and the captured variable live as long as the delegate to which the former is assigned.
VulpesRank #55 · 29.2K reputationPosted Sep 14, 2012, 1:16 AM EDT