Home About Lessons Blog On The Mic Contact Projects
Lessons BASIC · C#

C# Basics — Methods

8 min read · Emre Ulutabak
1
What is a method?

You woke up in the morning, brushed your teeth. Did it again at noon. And in the evening. You repeated the same action every time — but you only learned it once.

Code works the same way. Instead of writing the same thing over and over, you write it once, give it a name, and call it whenever needed. This is called a method.

csharp
static void Selamla()
{
    Console.WriteLine("Merhaba!");
}

Selamla();
Selamla();
Selamla();
💡
You wrote the method once, called it three times. If you want to change it, you only change one place.
2
Why use methods?

Code written without methods quickly becomes unreadable. Imagine copying the same block to 10 places — if you find a bug in one, you have to fix all 10.

Methods make code readable, maintainable, and reusable. These three properties are the foundation of good software.

3
Void vs Return

Some methods do something but don't return a value — these are void. Others produce something and give it back.

Think of a cook in the kitchen: a void cook makes the food but doesn't hand it to you. A return cook cooks it and hands you the plate.

csharp
static void Yazdir(string mesaj)
{
    Console.WriteLine(mesaj);
}

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

Yazdir("Merhaba");
int sonuc = Topla(3, 5);
Console.WriteLine(sonuc);
4
Parameters

You can pass information into methods from outside — these are called parameters. Think of a calculator: it doesn't just add, you also need to tell it which numbers to add.

csharp
static void Karşıla(string isim)
{
    Console.WriteLine($"Merhaba, {isim}!");
}

Karşıla("Emre");
Karşıla("Ayşe");
Karşıla("Mehmet");
💡
As the number of parameters increases, the method gets complex. If there are more than 3 parameters, reconsider the design.
5
Golden rules

There are some fundamental rules learned over years. Knowing these separates you from mid-level developers:

💡
A method should do only one thing. If it does more than one thing, split it.
💡
Method names should be verbs: Calculate(), Save(), Get() — what it does should be clear from the name.
💡
A method exceeding 20 lines is likely doing more than one thing. Review it.
MINI QUIZ
Which of the following is correct?
void methods always return a value
Every method must contain return
void methods don't return a value, they just do work
return is only used for strings