0

I had syntax like this and found it clunky, because of oh so many lines of code.

services.AddSwaggerDocument(_ => { ... });

So I decided to create a private method and invoke it from the fat-arrow statement like this.

services.AddSwaggerDocument(_ => ConfigSwag(_));
...
private ConfigSwag(SwaggerDocumentSettings settings) { ... }

It works and all's good. However, I do recall that I've seen a syntax for invoking a call that was parenthesis-less, like this.

services.AddSwaggerDocument(_ => ConfigSwag);

However, I can't remember where and I have no clue what it's called so I can't google it.

Am I just confused here or is there such a syntax? If it's the latter, what is it called and how do I need to alter the method to make it work?

DonkeyBanana
  • 3,266
  • 4
  • 26
  • 65
  • 2
    No, it was like this: `services.AddSwaggerDocument(ConfigSwag);` But don't use underscore as variable name unless you want to omit it's value. – ZorgoZ Dec 26 '18 at 17:23
  • Good question! I still closed it as a duplicate, because we have another question which explains in detail the differences between those two options. I am aware that you would not be able to find that other question without knowing what the syntax is called, so it's good to have your question as a duplicate on this site. – Heinzi Dec 26 '18 at 17:28
  • @ZorgoZ There it was! So stupid... Now, what is this syntax called? I'm pretty sure it's not *parentheis-less de-fat-arrowing*, hehe... And the underscore part - I only used it for brevity in the sample. In the method, I called the passed in variable *settings*, of course. – DonkeyBanana Dec 26 '18 at 17:29
  • This is a method group conversion, the rules of which are outlined in section 6.6 of the specification that ships with visual studio. – Jonathon Chase Dec 26 '18 at 17:30
  • @Heinzi Yupp, they are semantically very close. Yet, as you say, *the code to the safe is in the safe*. Good call and thanks for the polite explanation. – DonkeyBanana Dec 26 '18 at 17:32
  • @DonkeyBanana: I like *"parenthesis-less de-fat-arrowing"*, but it's an *implicit conversion of a method group to a delegate*: Your method expects a [delegate](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/), and you pass the name of a method, which is a [method group](https://stackoverflow.com/q/886822/87698) (it's a "group", because the name might refer to more than one method, because of overloading). – Heinzi Dec 26 '18 at 17:37

0 Answers0