1

How do I write a method with "=>" that is multi lined?

public int SimpleAddition(int firstInt, int secondInt) => firstInt + secondInt; 

If I am not mistaken, the above method is equivalent to:

public int SimpleAddition(int firstInt, int secondInt){ return firstInt + secondInt; } 

How would I write this method using "=>" if logic spans multiple lines?

public int SimpleAddition(int firstInt, int secondInt){ //do something else here// return firstInt + secondInt; } 
6
  • 1
    err, why not do the one without =>? if you need it for lambda stuff just return a Func<> instead, see this QA. Commented Dec 15, 2023 at 10:20
  • @BagusTesa I am kinda outdated with modern syntax. I could just write it regularly but I am just curious whether there is a more modern way of writing it. Also, since "=>" in lambda expressions can accommodate multi lines, I am wondering if it can be done in this situation as well. Commented Dec 15, 2023 at 10:22
  • Does this answer your question? Can a C# lambda expression have more than one statement? Commented Dec 15, 2023 at 10:23
  • the modern syntax doesn't necessarily shorter or better, you can treat them as syntactic sugar. var for instance, they are nice to use but kinda pain to work with when you have no access to proper IDE and only looking at codes on github, you have to dig more than you need to. Commented Dec 15, 2023 at 10:25
  • 2
    No, this => is different from the => in lambda expressions. This => is specifically made to simplify single-expression methods/getters/etc. While it is technically possible to write multiple lines with =>, by returning a new Func<int>(...)(), you'd end up writing more code than if you had done it the traditional way. Commented Dec 15, 2023 at 10:28

2 Answers 2

4

How would I write this method using "=>" if logic spans multiple lines?

You would not. The following:

public int SimpleAddition(int firstInt, int secondInt) => firstInt + secondInt; 

is expression-bodied method and expression-bodied members as the documentation states can consist only from single expression:

Expression body definitions let you provide a member's implementation in a concise, readable form. You can use an expression body definition whenever the logic for any supported member, such as a method or property, consists of a single expression.

Note that several chained method calls for example (like fluent APIs/LINQ) still form a single expression. For example:

public int SimpleAddition(int firstInt, int secondInt) => new[] { firstInt, secondInt } // just for example .Where(_ => true) .Sum(); 

So if you can rewrite your additional code in a way that it can be chained (or form a single expression) then you will be able to rewrite method to be expression-bodied (does not mean that you should though).

Sign up to request clarification or add additional context in comments.

Comments

0

you can use multi-line lambda expression (block lambda) using {}

example

using System; public class Program { // Example of a multi-line lambda expression (block lambda) Func<int, int, int> add = (a, b) => { int result = a + b; return result; }; public static void Main() { Program p = new Program(); Console.WriteLine(p.add(5, 5)); } } 

the function has 3 'int' here: two are refering to a and b, and the third is to the returned value

1 Comment

OP question was about expression-bodied methods NOT lambda expressions for anonymous functions. Both use => token, but they are very different language constructs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.