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.
static void Selamla()
{
Console.WriteLine("Merhaba!");
}
Selamla();
Selamla();
Selamla();
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.
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.
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);
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.
static void Karşıla(string isim)
{
Console.WriteLine($"Merhaba, {isim}!");
}
Karşıla("Emre");
Karşıla("Ayşe");
Karşıla("Mehmet");
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: