VuePressVuePress
Home
Get Started
Home
Get Started
  • Temel Kavramlar

    • Type Declarations (Tur Bildirimleri)
    • Access Modifiers (Erişim Değiştiricileri)
    • Erişimci (Accessor)
  • Metotlar ve İşlemler

    • Metotlar (Methods)
    • Geri Dönüş Türleri (Return Types)
    • Try-Catch Kullanımı
  • Veri Yapıları

    • Koleksiyon Arayüzleri (Collection Interfaces)
    • LINQ Sorguları
  • İleri Konular

    • Dependency Injection Servisleri
    • DbContext - OnModelCreating Metodu
  • DbContext ve Entity Framework

    • Entity Framework Core - Fluent API ve İlişkiler Rehberi

Erişimci (Accessor)

Nedir?

Erişimci, property'lerin değerlerine erişim kontrolü sağlayan get ve set bloklarıdır.

Erişimci Çeşitleri

1. get - Okuma

private int _age;

public int Age
{
    get { return _age; }
}

int age = person.Age; // Okuma

2. set - Yazma

private int _age;

public int Age
{
    set { _age = value; }
}

person.Age = 25; // Yazma

3. get ve set - Okuma/Yazma

private int _age;

public int Age
{
    get { return _age; }
    set { _age = value; }
}

4. Auto-Property

public string Name { get; set; }
public int Age { get; set; }

5. init - Başlatma (C# 9.0+)

public string Name { get; init; }

var person = new Person { Name = "Ali" };
person.Name = "Veli"; // Hata - değiştirilemez

6. private set - Sadece Sınıf İçinden Yazma

public string Email { get; private set; }

public void SetEmail(string email)
{
    Email = email; // Sadece sınıf içinden
}

person.Email = "test@gmail.com"; // Hata

7. protected set - Alt Sınıflardan Yazma

public virtual string Name { get; protected set; }

public class Employee : Person
{
    public void UpdateName(string name)
    {
        Name = name; // Alt sınıftan yazılabilir
    }
}

8. internal set - Proje İçinden Yazma

public string Code { get; internal set; }

// Aynı proje içinden yazılabilir
// Diğer projelerden yazılamaz

9. Doğrulama ile set

private int _age;

public int Age
{
    get { return _age; }
    set 
    { 
        if (value >= 0 && value <= 150)
            _age = value;
    }
}

10. Hesaplanmış Property

public string FirstName { get; set; }
public string LastName { get; set; }

public string FullName
{
    get { return $"{FirstName} {LastName}"; }
}

Erişimci Kombinasyonları

KombinasyonAçıklama
{ get; set; }Okuma ve yazma
{ get; }Sadece okuma
{ get; private set; }Dışarıdan okuma, içeriden yazma
{ get; init; }Okuma ve başlatma
{ get; protected set; }Okuma, alt sınıflardan yazma
{ get; internal set; }Okuma, proje içinden yazma

Erişimci Tablosu

ErişimciAçıklama
getProperty'den değer okunur
setProperty'ye değer atanır
initNesne oluşturulurken değer atanabilir, sonra değiştirilemez
private setSadece sınıf içinden atanabilir
protected setAlt sınıflardan atanabilir
internal setAynı proje içinden atanabilir
Last Updated:: 6/7/26, 12:33 PM
Contributors: yigitumretastan
Prev
Access Modifiers (Erişim Değiştiricileri)