What does this mean in C # or LINQ? - (() =>)

I went through a book by Jeffrey Palermo and came across this syntax.

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

What does it mean?

+4
source share
7 answers

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

+14
source

In general, this means a function with no arguments.

In this particular example, it creates an anonymous function with no arguments, which returns a new VisitorRepository(); object VisitorRepository(); everytime.

+4
source

Func<IVisitorRepository> means a delegate that takes no arguments and returns IVisitorRepository. Creating this delegate is a lambda function:

 () //means no parameters => new VisitorRepository()// means it returns a new VisitorRepository 
+2
source

() is the place where you place the variables

An example of a regular processed event will look like (sender, args)

=> // means throwing these parameters into this method

after => you can either remove single-line execution, for example new VisitorRepositor()

OR

you can place an entire function like

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

Like other declared lambda expressions, and it really clears your code of a method or function that processes a particular event.

Once you read them, it is really helpful.

+1
source

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

A delegate is defined using the generic func. Thus, in this case, the signature for the delegate: there are no input parameters and one output parameter of type IVisitorRepository.

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

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

+1
source

This means that the function does not accept any parameters, for example:

 delegate() {//} 
0
source

Func is a delegate without a parameter and with a return value of IVisitorRepository.

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

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

therefore, this line creates a delegate that points to an anonymous method that returns an instance of VisitorRepository.

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

In the next line, you set the value of the static property to the newly created delegate.

 VisitorRepositoryFactory.RepositoryBuilder = builder; 

After that, you can use the property to call an anonymous method that creates a new instance of VisitorRepository.

 IVisitorRepository repository = VisitorRepositoryFactory.RepositoryBuilder(); 

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

0
source

Source: https://habr.com/ru/post/1303255/


All Articles