I am interested if it\'s possible using C# to write a code analogous to this Javascript one:
var v = (function()
{
return \"some value\";
})()
Here's how you could then utilize such a construct to enclose context - closure-
Control.Click += new Func<string, EventHandler>((x) =>
new System.EventHandler(delegate(object sender, EventArgs e)
{
}))(valueForX);
Yes, but C# is statically-typed, so you need to specify a delegate type.
For example, using the constructor syntax:
var v = new Func<string>(() =>
{
return "some value";
})();
// shorter version
var v = new Func<string>(() => "some value")();
... or the cast syntax, which can get messy with too many parentheses :)
var v = ((Func<string>) (() =>
{
return "some value";
}))();
// shorter version
var v = ((Func<string>)(() => "some value"))();