views:

76

answers:

5
+4  Q: 

() => construct

I am in the process of converting a project from visual studio 2005 to visual studio 2008 and came up on the above construct.

using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using CommonServiceLocator.WindsorAdapter;
using Microsoft.Practices.ServiceLocation;

namespace MyClass.Business
{
    public class Global : System.Web.HttpApplication
    {
        public override void Init()
        {
            IServiceLocator injector =
                new WindsorServiceLocator(
                    new WindsorContainer(
                        new XmlInterpreter(
                            new ConfigResource("oauth.net.components"))));

            //ServiceLocator.SetLocatorProvider(() => injector);

            // ServiceLocator.SetLocatorProvider(injector);
        }
    }
}

ServiceLocator.SetLocatorProvider(() => injector);

Can I get an understanding of what this is.

A: 

It is a lambda expression that creates an anonymous delegate. That is, its a function declared inline. The parameter list is inside the paranthesis, so in this case there are no parameters. And when the function contains a single statement it implicitly returns the value of that statement (or returns nothing at all).

In this specific case, it is a function that returns the injector. This is a common pattern for a ServiceLocator, to initialize it with a function that returns the IoC container.

qstarin
An anonymous delegate would look more like `new delegate() { return injector; }`. Granted, the lamdba expression has the effect of creating an anonymous delegate in this case.
Steven Sudit
you're right, and I was in a hurry. I edited my answer to clarify the point better..
qstarin
+2  A: 

It's the lambda notation to create an inline delegate without parameter.

Timores
A: 

It is not a constructor but a Lamda expression. See here for more detail.

In this case () means no parameters => is the Lamda operator and injector is being returned.

David Basarab
+10  A: 

This is a lambda expression.

I guess that the SetLocatorProvider method has a signature like:

SetLocatorProvider( Func<IServiceLocator> callback ):

Now you have to provide such a callback. There are basically three options:

Use a method (always working):

private IServiceLocator GetServiceLocator() { /* return IServiceLocator */ }

ServiceLocator.SetLocatorProvider( GetServiceLocator() );

Use a delegate (requires C#2.0):

ServiceLocator.SetLocatorProvider( delegate
        {
            // return IServiceLocator
        } );

Use a lambda (requires C#3.0):
That's the code you see ...
Since there is no argument (Func<IServiceLocator> has only a return value) you specify this by using ():

ServiceLocator.SetLocatorProvider( () => { /* return IServiceLocator */ } );

this can be translated to

ServiceLocator.SetLocatorProvider( () => /* IServiceLocator */ );

Maybe you want to read this question + answer, too.

tanascius
+2  A: 

It's a lambda. If you're familiar with delegates, it's like declaring a method which returns injector and using that as a delegate, except you've inlined the method.

The first () contains the arguments to the lambda. For instance, in event handling, you'll often see (src, e) where src is the originator of the event and e is the event itself. The arguments are then available for the subsequent code to use.

If it's multiline, you can put (args) => { brackets } around the delegate and return the value. This is shorthand.

Lunivore
How do I convert it back to Visual Studio construct
ferronrsmith
It should work fine in VS2008. If you want it in old-style, turn the lambda into a method and use that as a delegate instead: ServiceLocator.SetLocatorProvider(CreateInjector);
Lunivore
@ferronrsmith - The actual .Net types for lambdas are the generic `Func<>` and `Action<>` types. The generic arguments represent the function arguments (and return type for Func<>). These are just special delegate classes which make things easier to use.
Mark H