Erişimci, property'lerin değerlerine erişim kontrolü sağlayan get ve set bloklarıdır.
private int _age;
public int Age
{
get { return _age; }
}
int age = person.Age;
private int _age;
public int Age
{
set { _age = value; }
}
person.Age = 25;
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
public string Name { get; set; }
public int Age { get; set; }
public string Name { get; init; }
var person = new Person { Name = "Ali" };
person.Name = "Veli";
public string Email { get; private set; }
public void SetEmail(string email)
{
Email = email;
}
person.Email = "test@gmail.com";
public virtual string Name { get; protected set; }
public class Employee : Person
{
public void UpdateName(string name)
{
Name = name;
}
}
public string Code { get; internal set; }
private int _age;
public int Age
{
get { return _age; }
set
{
if (value >= 0 && value <= 150)
_age = value;
}
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return $"{FirstName} {LastName}"; }
}
| Kombinasyon | Açı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 | Açıklama |
|---|
| get | Property'den değer okunur |
| set | Property'ye değer atanır |
| init | Nesne oluşturulurken değer atanabilir, sonra değiştirilemez |
| private set | Sadece sınıf içinden atanabilir |
| protected set | Alt sınıflardan atanabilir |
| internal set | Aynı proje içinden atanabilir |