-1

What name of ()=> //codes

waht does it do? or what is its purpose. Example in c#

 List<Task> Tasks = new List<Task>();
 var taskCustomer = Task.Factory.StartNew(() =>
 {
   using (NorthContext dbContext=new NorthContext())
   {
       model.CustomerList = dbContext.Customers.Where(
       cus=>cus.ContactName.Contains(search)).ToList();
   }
});
Tasks.Add(taskCustomer);

Example in js

window.addEventListener('resize', () => this.onResize()); 

this.onResize();

Edric
  • 24,639
  • 13
  • 81
  • 91

2 Answers2

4

In javascript/Typescript , Its called arrow function,

FROM DOCS

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

In C#,

It is called a lambda expression.

A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

This () => {//...} is called lambda expression in C#. It's an easy way to define a method and pass it as an argument to another mehtod. In this specific case the method you define has no parameters and does whatever you specify inside the curly braces. More formally as it is stated here:

A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions.

On the other hand, in JavaScript, this is called arrow function and as it is stated here:

An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

Christos
  • 53,228
  • 8
  • 76
  • 108