Thuta Learning
C#
IntermediateProgrammingBeginner

Methods

C#Lesson 1320 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Methods
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Methods, in plain language

Method is a reusable block of code. As a program grows, cramming everything into Main turns it into spaghetti fast. Breaking things into methods keeps your code easier to read, test, and change.

Problem → Why → Solution

The problem — why this concept matters

Combining input, UI, business logic, and storage in one method makes testing and reuse difficult.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.

Mental model
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Explain the core concept and runtime behavior of Methods

Core syntax or pattern

csharp
class Program
{
    static void SayHello(string name)
    {
        Console.WriteLine($"Hello, {name}!");
    }

    static int Add(int x, int y)
    {
        return x + y;
    }

    static void Main(string[] args)
    {
        SayHello("Aung");
        int total = Add(5, 3);
        Console.WriteLine($"Total: {total}");
    }
}

A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.

Read the code step by step

  1. typed inputclass Program

    Method is a reusable block of code. As a program grows, cramming everything into Main turns it into spaghetti fast. Breaking things into methods keeps your code easier to read, test, and change.

  2. C# expression and methodreturn x + y;

    A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.

  3. CLR value or object state}

    Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.

Execution flow
  1. typed input
  2. C# expression and method
  3. CLR value or object state

Success

Explain the core concept and runtime behavior of Methods

Failure

Combining input, UI, business logic, and storage in one method makes testing and reuse difficult.

Method is a reusable block of code. As a program grows, cramming everything into Main turns it into spaghetti fast. Breaking things into methods keeps your code easier to read, test, and change.

csharp
class Program
{
    static void SayHello(string name)
    {
        Console.WriteLine($"Hello, {name}!");
    }

    static int Add(int x, int y)
    {
        return x + y;
    }

    static void Main(string[] args)
    {
        SayHello("Aung");
        int total = Add(5, 3);
        Console.WriteLine($"Total: {total}");
    }
}

What this code does

  • SayHello method takes a name as a parameter and prints out a greeting.
  • Add method takes two numbers and returns their sum.
  • void means the method doesn't return a value.
  • int return type has to hand back an int value.
You should see
Hello, Aung! Total: 8

Real-world usage

How to apply it in a real project

Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.

Mini challenge

Build it yourself

Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
class Program
{
    static void SayHello(string name)
    {
        Console.WriteLine($"Hello, {name}!");
    }

    static int Add(int x, int y)
    {
        return x + y;
    }

    static void Main(string[] args)
    {
        SayHello("Aung");
        int total = Add(5, 3);
        Console.WriteLine($"Total: {total}");
    }
}

Expected: Explain the core concept and runtime behavior of Methods

Show hint

Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.

Quick quiz 1 / 3

What best describes the core idea of Methods?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • typed input → C# expression and method → CLR value or object state
  • A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.
  • Combining input, UI, business logic, and storage in one method makes testing and reuse difficult.
  • Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.

C# documentation — Microsoft Learn

Easy traps

  • Combining input, UI, business logic, and storage in one method makes testing and reuse difficult.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.

You'll know it worked when: Hello, Aung! Total: 8

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.