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

C# Temelleri — Generic Tipler C# Basics — Generics

9 dk okuma 9 min read · Emre Ulutabak
1
Generic nedir? What is a generic?

Bir kutu düşün. Bazen içine elma koyarsın, bazen kalem, bazen kitap. Kutunun kendisi değişmez — sadece içindeki değişir.

Generic de tam böyle çalışır. Aynı kodu farklı tipler için tekrar yazmak yerine, tipi dışarıdan parametre olarak alırsın.

Think of a box. Sometimes you put apples in it, sometimes pens, sometimes books. The box itself doesn't change — only its contents do.

Generics work exactly like this. Instead of rewriting the same code for different types, you take the type as a parameter from outside.

2
Neden kullanılır? Why use it?

Generic olmadan aynı işi int için de, string için de, başka bir tip için de ayrı ayrı yazmak gerekir. Bu hem tekrar hem de hata riskidir.

Generic yapılar tip güvenliğini korurken kodu yeniden kullanılabilir kılar.

Without generics, you'd need to write the same logic separately for int, string, and every other type. That's both repetition and risk of error.

Generic structures keep the code reusable while preserving type safety.

3
Generic metot Generic method
csharp
public T IlkElemaniGetir<T>(List<T> liste)
{
    if (liste.Count == 0)
        throw new InvalidOperationException("Liste boş.");

    return liste[0];
}

// Kullanım:
var sayilar = new List<int> { 10, 20, 30 };
var isimler = new List<string> { "Ali", "Veli", "Ayşe" };

Console.WriteLine(IlkElemaniGetir(sayilar));  // 10
Console.WriteLine(IlkElemaniGetir(isimler)); // Ali
💡
<T> ifadesi 'tip parametresi' anlamına gelir. T yerine istediğin ismi kullanabilirsin ama T en yaygın gelenektir. The <T> expression means 'type parameter'. You can use any name instead of T, but T is the most common convention.
4
Generic sınıf Generic class

Zaten kullandığın List<T> ve Dictionary<TKey, TValue> birer generic sınıftır. Kendi generic sınıfını da yazabilirsin.

The List<T> and Dictionary<TKey, TValue> you already use are generic classes. You can write your own generic class too.

csharp
public class Kutu<T>
{
    private T _icerik;

    public void Koy(T nesne)
    {
        _icerik = nesne;
    }

    public T Al()
    {
        return _icerik;
    }
}

// Kullanım:
var elmaliKutu = new Kutu<string>();
elmaliKutu.Koy("Elma");
Console.WriteLine(elmaliKutu.Al()); // Elma

var sayiKutu = new Kutu<int>();
sayiKutu.Koy(42);
Console.WriteLine(sayiKutu.Al()); // 42
5
Altın kurallar Golden rules
💡
Generic yapılar tip güvenlidir. Yanlış tip verirsen derleme hatası alırsın, runtime'da değil. Generic structures are type-safe. If you pass the wrong type, you get a compile error — not a runtime crash.
💡
List<T>, Dictionary<K,V>, IRepository<T> — bunların hepsi generic. Kullandığın her yerde bu mantık var. List<T>, Dictionary<K,V>, IRepository<T> — all of these are generic. This pattern is everywhere you look.
💡
Generic'i 'tekrar yazmamak için şablon' olarak düşün. Think of generics as a template to avoid rewriting the same code.
MİNİ QUIZ MINI QUIZ
Generic tipler hakkında hangisi doğrudur? Which is correct about generic types?
Generic yapılar yalnızca int tipi için çalışır Generic structures only work with int
T harfi zorunludur, başka harf kullanılamaz The letter T is mandatory and cannot be changed
Generic yapılar tip güvenliğini korur ve kodu yeniden kullanılabilir kılar Generic structures preserve type safety and make code reusable
List<T> generic bir yapı değildir List<T> is not a generic structure