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

Method Basics

A method is a named set of steps you can reuse, like a recipe card. You write it once and run it whenever you need it.

In this page:

  1. Method Basics
Syntax
csharp
static ReturnType MethodName()
{
    // body
}

MethodName();   // call it

Method Basics

A method is a named, reusable block of code declared with a return type, a name, and a parameter list. Calling a method transfers control to its body and resumes the caller once it returns.

Breaking logic into methods keeps programs organized and avoids repeating code.

Note: Method names conventionally use PascalCase in C#, like CalculateTotal.

Example: Method Basics

markup
using System;

class Program
{
    static void PrintBanner()
    {
        Console.WriteLine("=== Welcome ===");
    }

    static void Main(string[] args)
    {
        PrintBanner();
        Console.WriteLine("Program running");
    }
}
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. Forgetting return in a method with a non-void return type, which is a compile error.
  2. Calling a non-static method from Main without creating an object.
  3. Forgetting the parentheses when calling a method, so Print; does not call it.
🔒

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.