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

Return Types

In this page:

  1. Return Types

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);
    }
}
🔒

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.