Ana Sayfa Hakkımda Dersler Blog MikrofondaOn The Mic İletişim Projeler Yakında
DerslerLessons TEMEL · C#

C# Temelleri — Metotlar C# Basics — Methods

8 dk okuma 8 min read · Emre Ulutabak
1
Metot nedir? What is a method?

Sabah kalktın, dişlerini fırçaladın. Öğlen yine fırçaladın. Akşam da. Her seferinde aynı hareketi tekrarladın — ama bu hareketi bir kez öğrendin.

Kod da böyle çalışır. Aynı işi defalarca yazmak yerine bir kez yazarsın, bir isim verirsin, gerektiğinde çağırırsın. Bu yapıya metot denir.

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();
💡
Metodu bir kez yazdın, üç kez çağırdın. Kodu değiştirmek istersen sadece bir yeri değiştirirsin. You wrote the method once, called it three times. If you want to change it, you only change one place.
2
Neden kullanırız? Why use methods?

Metot kullanmadan yazılan kod kısa sürede okunaksız hale gelir. Aynı kod bloğunu 10 yere kopyaladığını düşün — birinde hata bulursan 10 yeri düzeltmen gerekir.

Metotlar kodu okunabilir, bakımı kolay ve yeniden kullanılabilir yapar. Bu üç özellik iyi yazılımın temelidir.

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 Void vs Return

Bazı metotlar bir şey yapar ama geri dönmez — bunlar void'dur. Bazıları ise bir şey üretir ve geri verir.

Mutfakta yemek yapan biri gibi düşün: void aşçı yemeği pişirir ama sana vermez. Return aşçı ise pişirip tabağı uzatır.

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
Parametreler Parameters

Metotlara dışarıdan bilgi verebilirsin — bunlara parametre denir. Bir hesap makinesi düşün: sadece toplamak için değil, hangi sayıları toplayacağını da söylemen gerekir.

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");
💡
Parametre sayısı arttıkça metot karmaşıklaşır. 3'ten fazla parametre varsa durumu yeniden düşün. As the number of parameters increases, the method gets complex. If there are more than 3 parameters, reconsider the design.
5
Altın kurallar Golden rules

Yıllar içinde öğrenilen bazı temel kurallar var. Bunları bilmek seni orta seviye yazılımcıdan ayırır:

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

💡
Bir metot sadece bir iş yapmalı. Birden fazla iş yapıyorsa ikiye böl. A method should do only one thing. If it does more than one thing, split it.
💡
Metot ismi fiil olmalı: Hesapla(), Kaydet(), Getir() — ne yaptığı isimden anlaşılmalı. Method names should be verbs: Calculate(), Save(), Get() — what it does should be clear from the name.
💡
20 satırı geçen metot büyük ihtimal birden fazla iş yapıyordur. Gözden geçir. A method exceeding 20 lines is likely doing more than one thing. Review it.
MİNİ QUIZ MINI QUIZ
Aşağıdakilerden hangisi doğrudur? Which of the following is correct?
void metotlar her zaman bir değer döndürür void methods always return a value
Her metot return içermek zorundadır Every method must contain return
void metotlar değer döndürmez, sadece iş yapar void methods don't return a value, they just do work
return sadece string için kullanılır return is only used for strings