1

I have the following method:

protected Func<T> GetMenuItems<T>() where T : IMenuItem, new()
{
    return () => _menuItems.GetMenuItem<T>();
}

It's the first time that I ever saw this syntax: return () => _menuItems.GetMenuItem<T>() what are these parenthesis for and what do they do?

O S
  • 453
  • 3
  • 14
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions – CodeCaster Dec 10 '19 at 15:09
  • 2
    It's returning a delegate of type `Func` The body of the delegate being returned is `_menuItems.GetMenuItem();` The parentheses and the arrow operator are a *lambda expression.* The empty parentheses indicate that the lambda expression takes no parameters. – Robert Harvey Dec 10 '19 at 15:09
  • 1
    Lambda function. Could also do something like `() => { return _menuItems.GetMenuItem(); }` – 3Dave Dec 10 '19 at 15:10
  • [Explain what's the use of this statement += () => and how it works](https://stackoverflow.com/q/52206316/995714), [What does the '=>' syntax in C# mean?](https://stackoverflow.com/q/290061/995714), [What does SomeMethod(() => x.Something) mean in C#](https://stackoverflow.com/q/1370236/995714) – phuclv Dec 10 '19 at 15:13
  • Thanks for the comments and answers, I think I need to first learn delegates to understand this. – O S Dec 10 '19 at 15:23

2 Answers2

3

It returns the whole expression () => _menuItems.GetMenuItem<T>(); which is a lambda notation :

A function taking no parameter () (think of Foo()), returning the result of _menuItems.GetMenuItem<T>().

It is useful to return this way for instance to do "Lazy execution" : the function GetMenuItem is not yet executed. However the Func<T> returned has all the information to execute this inner function.

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • 1
    Doesn't this capture `_menuItems`? So it may occur, that `_menuItems` (the object it references) lives longer than the object where it is from (if this makes sense)? – Fildor Dec 10 '19 at 15:18
  • 2
    @Fildor you should indeed be careful with this. This will prevent Garbage collector to clean up some resources. But I don't think you will get any trouble about the lifetime of the object in classic cases, apart from retaining resources too long. Maybe I understand it wrong, but that's what the "managed" part of using a managed language permit us to do. I picture you create object "on the heap", and they are cleaned if no more reference to them exist. Have this Func still being referenced, mean the object is still referenced as well. – Pac0 Dec 10 '19 at 15:23
  • Thanks for confirming. I was just wondering and thought "this may lead to some interesting debugging sessions". But as you say, it's a feature and you should of course always know what you are doing. – Fildor Dec 10 '19 at 15:24
2

It means that returns a function with no parameters "()" which body is

{
     return _menuItems.GetMenuItem<T>();
}

You can also return functions with parameters in a similar fashion:

return (int a) => a++;
Juan
  • 3,675
  • 20
  • 34