9 November 2025 Sunday 10:43:17


C# 6.0 İle Expression Bodied Functions and Properties

C# 6.0 İle Expression Bodied Functions and Properties

Expression Bodied Functions, method ve property gövdelerini lambda ifadesi (=>) ile tanımlamak için kullanılmaktadır. C# 6.0 öncesinde aşağıdaki gibi bir kod yazalım.

public class Personel  
{  
    public Personel()  
    {  
        this.Ad = "Ali";  
        this.Soyad = "Can";  
    }  
  
    public string Ad { get; }  
  
    public string Soyad { get;  }  
  
    public string TamAd  
    {  
        get  
        {  
            return string.Format("{0} {1}", Ad, Soyad);  
        }  
    }  
}  

Benzer örneği C# 6.0 ile yapalım.

public class Personel  
{  
    public string Ad { get; } = "Ali";  
  
    public string Soyad { get; } = "Can";  
  
    public string TamAd => $"{Ad} {Soyad}";  
}  

"TamAd" property tanımına dikkat ederseniz kod gövdesi süslü parantezler ile sınırlandırılmamıştır. Metod gövdesi lambda (=>) ifadesi ile oluşturulmuştur. Benzer örneği bir fonksiyon için yapalım.

public int Hesapla(int sayi1, int sayi2)  
{  
    return sayi1 + sayi2;  
}  

C# 6.0 ile aşağıdaki gibi yazabiliriz.

public int Hesapla(int sayi1, int sayi2) => sayi1 + sayi2;      

Son olarak metod için bir örnek yapalım.

public class Personel  
{  
    public string Ad { get; set; }  
  
    public string Soyad { get; set; }  
  
    public void Bilgi()  
    {  
        Console.WriteLine(string.Format("{0} {1}", Ad, Soyad));  
    }  
}  

C# 6.0 ile örnekleyelim.

public class Personel  
{  
    public string Ad { get; set; }  
  
    public string Soyad { get; set; }  
  
    public void Bilgi() => Console.WriteLine($"{Ad} {Soyad}");  
}  

 

img

ibrahim ÖZKAN