Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Method Return Values | Methods
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

book
Method Return Values

In the last two chapters, we learned how to pass data into the functions but now we will learn how to retrieve data from the method back to the place where it was executed.

The process of retrieving data from methods is also called returning data and the data or value which is returned is called the return value.

The syntax for creating a method with a return value is the following:

cs

main

copy
1234
// Note: Parameters are optional static returnDataType methodName(dataType parameter1, ...) { return valueToReturn; }

The valueToReturn represents a variable, value, or expression which must be of the same type as the returnDataType. Following is the correct example:

cs

main

copy
1234
static int sumOfThree(int a, int b, int c) { int sum = a + b + c; return sum; }

If the wrong type of data is returned, it will show an error:

cs

main

copy
1234
static int sumOfThree(int a, int b, int c) { string sum = "10"; return sum; // Error (the string has a number in it, but it is still a string/text) }

The value which is returned from the sumOfThree method can be stored into a variable:

cs

main

copy
123456789101112131415161718
using System; namespace ConsoleApp { internal class Program { static int sumOfThree(int a, int b, int c) { int sum = a + b + c; return sum; } static void Main(string[] args) { int result = sumOfThree(5, 10, 15); Console.WriteLine(result); // Output: 30 } } }

We can also directly output the return value using Console.WriteLine:

cs

main

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static int sumOfThree(int a, int b, int c) { int sum = a + b + c; return sum; } static void Main(string[] args) { Console.WriteLine(sumOfThree(5, 10, 15)); } } }

We can also directly write expressions as return values. In that case, the expression is first evaluated and then the resulting value is returned. Following are some examples:

Product of Three Integers:

cs

main

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static int productOfThree(int a, int b, int c) { return a * b * c; } static void Main(string[] args) { Console.WriteLine(productOfThree(5, 10, 15)); } } }

Average:

cs

main

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static float average(int a, int b) { return (a + b) / 2.0f; } static void Main(string[] args) { Console.WriteLine(average(5, 10)); } } }
What should be the return data type of the following method?

What should be the return data type of the following method?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 6. Chapter 6
We're sorry to hear that something went wrong. What happened?
some-alt