Class Yapılarında set; get;
Get (almak, elde etmek) ve set (düzenlemek, ayarlamak) şeklinde iki ayrı alt metodu bulunur.
get Metodu: Bir değer döndürmek için kullanılır. Özelliklerin get metodunda return anahtar kelimesi kullanılarak “return…;” ile bir değerin döndürüleceği belirtilir.
set Metodu: Değişkene değer atama işlemleri için kullanılır. Burada görülen value anahtar kelimesi dışarıdan bu özelliğe gönderilen değeri temsil eder.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Ucgen u = new Ucgen(3, 4, 5);
Ucgen u = new Ucgen
{
A = 3,
B = 4,
C = 5
};
}
}
public class Ucgen
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
/*public Ucgen(int A, int B, int C)
{
this.A = A;
this.B = B;
this.C = C;
}*/
}
{
public MainWindow()
{
InitializeComponent();
//Ucgen u = new Ucgen(3, 4, 5);
Ucgen u = new Ucgen
{
A = 3,
B = 4,
C = 5
};
}
}
public class Ucgen
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
/*public Ucgen(int A, int B, int C)
{
this.A = A;
this.B = B;
this.C = C;
}*/
}
Örnek
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Person Kisi = new Person();
Kisi.Name = "Ali";
MessageBox.Show(Kisi.Name);
}
}
class Person
{
private string name; // field
public string Name // property
{
get { return name; }
set { name = value; }
}
}
Örnek
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Ogrenci Ogr = new Ogrenci();
Ogr.Yas = -13;
MessageBox.Show("" + Ogr.Yas);
}
}
public class Ogrenci
{
private int KorunmusYas=5;
public int KisininYasi;
public int Yas
{
get { return KisininYasi; }
set
{
if (value > 0)
KisininYasi = value;
else
{
MessageBox.Show("Yaş sıfırdan küçük olmamalı");
KisininYasi = 0;
}
}
}
/*public int Yas
{
get { return KorunmusYas; }
set
{
if (value > 0)
KorunmusYas = value;
}
}*/
}