Day 7: Methods - Stop Repeating Yourself, Let Functions Do the Work
arrow_back All Posts
April 06, 2026 5 min read .NET/C#

Day 7: Methods - Stop Repeating Yourself, Let Functions Do the Work

Welcome back! On Day 6 you learned to store entire collections of data in arrays, lists, and dictionaries. But your Main method is starting to look like a novel. Today we learn how to chop your code into named, reusable pieces called methods. Think of it as hiring employees instead of doing everything yourself.

1. What Is a Method?

A method is a named block of code that does one specific job. You define it once, then call it whenever you need that job done.

You've been calling methods since Day 1 — Console.WriteLine() is a method. Someone at Microsoft wrote it once, and now millions of developers call it every day. Today you write your own.

Think of a method like a vending machine: you put something in (parameters), it does work inside, and it gives something back (return value). Or sometimes it just does something and gives nothing back — like a machine that prints receipts.

2. Your First Method

static void SayHello()
{
    Console.WriteLine("Hello from a method!");
}

// Call it from Main:
SayHello();
SayHello();
SayHello();

Output:

Hello from a method!
Hello from a method!
Hello from a method!

Three calls, one definition. That's the whole point — write once, call many.

Breaking down the signature:

  • static — means it belongs to the class, not to an instance. Don't worry about this yet — we'll explain it when we cover classes on Day 10.
  • void — means the method returns nothing. It just does stuff.
  • SayHello — the method name. PascalCase, always.
  • () — empty parentheses mean it takes no inputs.

3. Parameters — Giving Methods Inputs

Most methods need data to work with. You pass data in through parameters:

static void Greet(string name)
{
    Console.WriteLine($"Hello, {name}!");
}

Greet("Alice");    // Hello, Alice!
Greet("Bob");      // Hello, Bob!

name is a parameter (the variable in the definition). "Alice" is an argument (the actual value you pass in). People use these words interchangeably — don't stress about it.

Multiple parameters? Separate them with commas:

static void Introduce(string name, int age)
{
    Console.WriteLine($"I'm {name}, {age} years old.");
}

Introduce("Charlie", 25);

4. Return Values — Getting Results Back

void methods do things but don't hand anything back. Often you want a method to calculate something and return the result:

static int Add(int a, int b)
{
    return a + b;
}

int result = Add(3, 5);
Console.WriteLine(result);  // 8

The return type (int) replaces void. The return keyword sends the value back to the caller and immediately exits the method.

static string GetGrade(int score)
{
    return score switch
    {
        >= 90 => "A",
        >= 80 => "B",
        >= 70 => "C",
        >= 60 => "D",
        _ => "F"
    };
}

Console.WriteLine(GetGrade(85));  // B

Look — we pulled the grading logic from Day 6's homework into a reusable method. Now any part of your program can grade a score without copy-pasting the switch.

5. Expression-Bodied Methods — The One-Liner

When a method has just a single expression, you can skip the braces and use the => arrow:

static int Square(int n) => n * n;
static bool IsEven(int n) => n % 2 == 0;
static string Shout(string text) => text.ToUpper() + "!!!";

Console.WriteLine(Square(4));       // 16
Console.WriteLine(IsEven(7));       // False
Console.WriteLine(Shout("hello"));  // HELLO!!!

Same behavior as the full-body version, just shorter. Use these for simple methods — don't cram complex logic into a one-liner.

6. Optional Parameters and Default Values

Sometimes a parameter has a sensible default. Instead of forcing the caller to always provide it, give it a default value:

static void Log(string message, string level = "INFO")
{
    Console.WriteLine($"[{level}] {message}");
}

Log("Server started");           // [INFO] Server started
Log("Disk full", "WARNING");     // [WARNING] Disk full
Log("Crash!", "ERROR");          // [ERROR] Crash!

Default parameters must come after required parameters. (string level = "INFO", string message) won't compile — the compiler needs to know which arguments go where.

7. Method Overloading — Same Name, Different Inputs

You can have multiple methods with the same name but different parameter lists. C# picks the right one based on what you pass in:

static int Multiply(int a, int b) => a * b;
static double Multiply(double a, double b) => a * b;
static int Multiply(int a, int b, int c) => a * b * c;

Console.WriteLine(Multiply(3, 4));        // 12 (int version)
Console.WriteLine(Multiply(2.5, 3.0));    // 7.5 (double version)
Console.WriteLine(Multiply(2, 3, 4));     // 24 (three-param version)

This is called overloading. Console.WriteLine itself has 18 overloads — one for string, one for int, one for bool, etc. That's why you can print anything without converting it first.

8. Your Homework: Build a Mini Calculator

Create a console app with separate methods for Add, Subtract, Multiply, and Divide. Ask the user for two numbers and an operation, then call the right method.

static double Add(double a, double b) => a + b;
static double Subtract(double a, double b) => a - b;
static double Multiply(double a, double b) => a * b;
static double Divide(double a, double b) => b != 0 ? a / b : double.NaN;

Console.Write("First number: ");
double num1 = double.Parse(Console.ReadLine() ?? "0");

Console.Write("Second number: ");
double num2 = double.Parse(Console.ReadLine() ?? "0");

Console.Write("Operation (+, -, *, /): ");
string op = Console.ReadLine() ?? "";

double answer = op switch
{
    "+" => Add(num1, num2),
    "-" => Subtract(num1, num2),
    "*" => Multiply(num1, num2),
    "/" => Divide(num1, num2),
    _ => double.NaN
};

Console.WriteLine($"Result: {answer}");

Notice Divide checks for zero before dividing — b != 0 ? a / b : double.NaN uses the ternary from Day 3. double.NaN means "Not a Number" — a safe way to signal that the math doesn't work.

Bonus challenge: add a static double Power(double baseNum, int exponent) method that calculates baseNum raised to exponent using a for loop (no Math.Pow allowed!).

For the full reference, see the C# methods docs.

Summary of Day 7

  • A method is a named, reusable block of code. Define once, call anywhere.
  • void methods do work but return nothing. Other methods specify a return type (int, string, etc.) and use return.
  • Parameters let you pass data in. Arguments are the actual values.
  • Expression-bodied (=>) methods are a one-liner shortcut.
  • Default parameters give callers the option to skip arguments that have sensible defaults.
  • Overloading means same name, different parameter lists — C# picks the right one automatically.
  • Methods make code reusable, readable, and testable. If you're copy-pasting code, you need a method.

Tomorrow: we'll talk about Strings in Depth — all the tricks for searching, slicing, formatting, and comparing text. Strings are everywhere, and C# has a surprisingly rich toolbox for them. 🔤

See you on Day 8!

Share
FM

Farhad Mammadov

.NET Engineer & Cloud Architect · Bayern, Germany. Writing about scalable backend systems, AWS, and SRE.