codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

15 / 60 topics
13Methods in C#14Parameters and Arguments15Return Values from Methods16Recursion in C#
Tutorials/C# Programming/Return Values from Methods
🔷C# Programming

Return Values from Methods

Updated 2026-05-15
10 min read

Return Values from Methods

Introduction

In C#, methods are blocks of code that perform specific tasks and can be reused throughout your program. One of the key features of methods is their ability to return data back to the caller. This tutorial will guide you through understanding how to return values from methods, which is essential for building robust and functional applications.

Concept

When a method executes its task, it can optionally return a value to the code that called it. The type of the returned value is specified in the method's signature using the return keyword followed by the value to be returned. Methods that do not return any value are marked with the void keyword.

Key Points

  • Return Type: Every method must specify a return type, which indicates the type of data it will return.
  • Return Statement: The return statement is used to exit a method and return a value to the caller.
  • Void Methods: Methods that do not return any value are declared with the void keyword.

Examples

Let's explore some practical examples to understand how methods can return values.

Example 1: Returning an Integer

In this example, we will create a method that adds two integers and returns their sum.

csharp
1public class MathOperations
2{
3 public int Add(int a, int b)
4 {
5 return a + b;
6 }
7}
8
9class Program
10{
11 static void Main()
12 {
13 MathOperations operations = new MathOperations();
14 int result = operations.Add(5, 3);
15 Console.WriteLine("The sum is: " + result); // Output: The sum is: 8
16 }
17}

Example 2: Returning a String

Here, we will create a method that concatenates two strings and returns the result.

csharp
1public class StringOperations
2{
3 public string Concatenate(string str1, string str2)
4 {
5 return str1 + " " + str2;
6 }
7}
8
9class Program
10{
11 static void Main()
12 {
13 StringOperations operations = new StringOperations();
14 string result = operations.Concatenate("Hello", "World");
15 Console.WriteLine(result); // Output: Hello World
16 }
17}

Example 3: Returning a Custom Object

In this example, we will create a method that returns an instance of a custom class.

csharp
1public class Person
2{
3 public string Name { get; set; }
4 public int Age { get; set; }
5
6 public Person(string name, int age)
7 {
8 Name = name;
9 Age = age;
10 }
11}
12
13public class PersonFactory
14{
15 public Person CreatePerson(string name, int age)
16 {
17 return new Person(name, age);
18 }
19}
20
21class Program
22{
23 static void Main()
24 {
25 PersonFactory factory = new PersonFactory();
26 Person person = factory.CreatePerson("John Doe", 30);
27 Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); // Output: Name: John Doe, Age: 30
28 }
29}

Example 4: Void Methods

Methods that do not return any value are declared with the void keyword. They are useful for performing actions without needing to return a result.

csharp
1public class Printer
2{
3 public void PrintMessage(string message)
4 {
5 Console.WriteLine(message);
6 }
7}
8
9class Program
10{
11 static void Main()
12 {
13 Printer printer = new Printer();
14 printer.PrintMessage("Hello, C#!"); // Output: Hello, C#!
15 }
16}

What's Next?

Now that you have a good understanding of how to return values from methods in C#, the next step is to explore Recursion in C#. Recursion is a powerful technique where a method calls itself to solve smaller instances of the same problem, which can be very useful for tasks like traversing data structures or solving complex algorithms.

Happy coding!


PreviousParameters and ArgumentsNext Recursion in C#

Recommended Gear

Parameters and ArgumentsRecursion in C#