← Back to C# Course | Chapter 5: Methods | Lesson 3 of 6

Return Types

A return type says what kind of answer a method gives back, like a vending machine promising a drink or a snack. Some methods return nothing at all, which is called void.

In this page:

  1. Return Types
Syntax
csharp
static ReturnType MethodName(type parameter)
{
    return value;
}

ReturnType result = MethodName(argument);

Return Types

A method's return type declares what kind of value it hands back to its caller with the return keyword. A method that returns nothing uses void. Every code path in a non-void method must return a value, or the compiler raises an error.

Warning: Leaving out a return on some code path of a non-void method is a compile error, not a runtime one.

Example: Return Types

markup
using System;

class Program
{
    static int Square(int n)
    {
        return n * n;
    }

    static void Main(string[] args)
    {
        int result = Square(6);
        Console.WriteLine(result);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Returning a value from a void method, which is a compile error.
  2. Forgetting that all code paths must return a value in a non-void method.
  3. Returning null for a value type such as int without using int?.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.