Home >> Desktop >> Delegates in C#
C# ile Delegate (Temsilci) Kullanımı - The Kod Program
C# ile Delegate (Temsilci) Kullanımı - The Kod Program

Delegates in C#

We are gonna talk about delegates in C#. Our program will have much power and will be efficient with this objects.

Delegates in C# is similar to pointers in C and C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.

We can use these classes to call external methods and so we can call back these external methods. We need to add the System. Delegate library to our project.

Declerating Delegate Objects

With delegate a method can be bounded to a referance. With doing this a delegate represent a method. So to call a method we use this delegate object. When we call this delegate, this method will be called back.

There is a delegate returns int and accepts string values declerated below

Here is the template of delegating:

How to start a delegate ?

Before delegate started, it must be declerated with new keyword and must have relation with function. After declerating this delegate variable, calling this variables will go to this main function. Below code block have example about declerating delegate object to function.

Below code block have examples about declerating and calling delegate object. We can send parameter and it returns some value.

Result of the code block above is : 

Multipling the delegate object

Delegate nesneleri + işareti ile birleştirilebilir. Birleştirilmiş olan delegate nesnesi çağrılan fonksiyonları birleştirilmiş şekilde çağırır. Sadece aynı türden olan delegate nesneleri birleştirilebir. Yani int ile int string ile string bool ile bool vs. delegate neslneleri birleştirilebilir. – işareti ise birleştirilmiş olan delegate nesnesinden bileşen çıkarmak ayrıştırmak için kullanılır.

Bazen yazılım içerisindeki isteme işlemlerinde çoklama veya azlama işlemleri kullanılabilir.Buna terimsel olarak multicasing denir. Aşağıda buna bir örnek yapalım.

First we equalized d1 to d and added 5 first result is 15. Then we multiplied this result with 5 with second delegate function and the result is the 75.

Example of delegate usage

Now mates, Please lastly let me make a last example about delegates and finish the article.

Result of the above code block is the below:

Now we know how to declare delegate objects and call it. We have made some examples about delegates.

We can use delegates to power up our program and make it more efficient.

That’s it in this article for now. Keep in touch.

Burak Hamdi TUFAN

Good works for everyone.

About Burak Hamdi TUFAN

I am a software developer experienced 12 years and here to share all my programming experiences. I have worked on so many platforms and programming languages especially C, C#, C++ and Java. I am studing PhD at Kocaeli University on Aviation Technologies. I am building softwares and technologies on aviation.

Leave a Reply

Your email address will not be published. Required fields are marked *