Back to course

Return Values from Methods

C# Zero to Hero: Comprehensive Programming Masterclass

Returning Data

If you want a method to return a value, you use a data type (like int or string) instead of void, and use the return keyword.

Example

csharp static int AddNumbers(int x, int y) { return x + y; }

static void Main(string[] args) { int result = AddNumbers(5, 3); Console.WriteLine(result); // 8 }

When the program reaches return, the method stops executing.