0

Let's say I have a simple function that I can write in several ways:


// Using a proper function body
private static string FormatViewTitle([CanBeNull] string title)
{
    return title is not null? string.Join(' ', ViewTitlePrefix, "-", title) : ViewTitlePrefix;
}


// Using a lambda expresion
private static string FormatViewTitle([CanBeNull] string title) =>
    title is not null
        ? string.Join(' ', ViewTitlePrefix, "-", title)
        : ViewTitlePrefix;

Does the compiler do extra optimizations if using the latter?

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
  • 5
    The second example isn't a Lambda - it's an expression-bodied method: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members#methods – Martin Costello May 10 '22 at 10:32
  • 4
    The second is not a lambda method. The two methods are identical. Here, the symbol => is used only for cosmetic/practical reasons. – Rubidium 37 May 10 '22 at 10:32
  • 2
    Why don't you compare the lowered `C#` or the IL on [SharpLab](https://sharplab.io/) – WBuck May 10 '22 at 10:54
  • Thanks for the clarifications! I loved SharpLab @WBuck! Thanks y'all! – Some random IT boy May 10 '22 at 11:55

0 Answers0