tags:

views:

212

answers:

7

I was going through Jeffrey Palermo book and came across this syntax.

private void InitializeRepositories()  
{
    Func<IVisitorRepository> builder =  () => new VisitorRepository();
    VisitorRepositoryFactory.RepositoryBuilder = builder;
}

Any help?

+4  A: 

In general it means function with no args.

It creates a anonymous function with no arguments that returns new VisitorRepository(); object every time in this particular example.

anthares
+12  A: 

() => indicates a lambda expression that takes no arguments.

dtb
I like Python's version of lambda better: `builder = lambda: VisitorRepository()`.
Hamish Grubijan
I like javascript's version better: function() VisitorRepository().
Jimmy
I like Alonzo Church's version of lambda better: λ
Eric Lippert
A: 

means a function takes no parameter. like: delegate() {//}

Benny
+2  A: 

Func<IVisitorRepository> stands for a delegate which takes no arguments and returns a IVisitorRepository. The creation of that delegate is a lambda function:

() //means no parameters
=> new VisitorRepository()// means it returns a new VisitorRepository
Yuriy Faktorovich
A: 

Func is a delegate without parmeter and with an IVisitorRepository return value.

() => is a lambda expression creating an anonymous method.

new VisitorRepository() is the content of this anonymous method.

so this line is creating a delegate which is pointing to a anonymous method, which is returning an instance of VisitorRepository.

Func<IVisitorRepository> builder =  () => new VisitorRepository()

In the next line, you set the value of a static property to this just created delegate.

VisitorRepositoryFactory.RepositoryBuilder = builder;

After this you can use the property to call the anonymous method, which is creating a new instance of VisitorRepository.

IVisitorRepository repository = VisitorRepositoryFactory.RepositoryBuilder();

In this case, repository will be an instance of VisitorRepository.

Enyra
A: 

() is the place where you place your variables

Example a common event handled would look like (sender, args)

=> // means throw these parameter into this method

after => you can either drop a one line execution thing like new VisitorRepositor()

OR

you can place a whole function like

Func<IRepository> = (sender, args) => 
{
    var myObject = (SomeObject)sender;
    return new VisitorReposiroty { id = myObject.SomeId };
}

As other stated it's lambda expression and it really clears your code from method or function that handle a specific event.

Once you read them good it's really damn useful.

NPayette
A: 

The () => syntax is a lambda expression. Lambdas were introduced in C# 3.0 and are used to define an anonymous method for a delegate.

The delegate is defined using the generic Func. So in this case the signature for the delegate is: no input parameters and one output parameter of type IVisitorRepository.

So on the left side of the => lambda array are the names of the input parameters. In case of no input parameters just write (). On the rightside of the => lambda is the code to return the output parameter, in this example: new VisitorRepository().

I suggest read more about lambda expressions in C# to fully understand this code. There is also a generic delegate involved, so you need understanding of Generics and Delegates as well.

Erikito