Learn Object Oriented Programming Using C#: Part 13

Before reading this article, please go through the following articles:

  1. Object Oriented Programming Using C#: Part 1
  2. Object Oriented Programming Using C#: Part 2
  3. Object Oriented Programming Using C#: Part 3
  4. Object Oriented Programming Using C#: Part 4
  5. Object Oriented Programming Using C#: Part 5
  6. Object Oriented Programming Using C#: Part 6
  7. Object Oriented Programming Using C#: Part 7
  8. Object Oriented Programming Using C#: Part 8
  9. Object Oriented Programming Using C#: Part 9
  10. Object Oriented Programming Using C#: Part 10
  11. Learn OOP Interface Using C#
  12. Learn Object Oriented Programming Using C#: Part 12

Delegates

Dear reader, today we will discuss another important component of OOP, which is delegates. It's the modern approach to call methods. Using that approach you can call any method with the same signature as the delegate. So we can say that a delegate is the launching pad for methods; any method can be launched that matches the pattern. Or a Delegate is a class. When you create an instance of it, you pass in the function name (as a parameter for the delegate's constructor) to which this delegate will refer.

In simple words, a delegate is a type that safely encapsulates a method; a delegate is a pointer to a method. Just like you can pass a variable by reference, you can pass a reference to a method. Delegates are often used to implement callbacks and event listeners. A delegate does not need to know anything about the classes or methods it works with.

A Delegate consists of the following three parts:

  • Declare a Delegate
  • Instantiate the Delegate
  • Execute the Delegate

    OOP1.jpg

Simple Example 1:

OOP2.jpg

Output:

OOP3.jpg

Block A:

  • In this sessoion we have defined a delegate with the name Delegate_Pro with two parameters.

Block B:

  • In this sessoion we have defined the method totalcost with parameters.
  • In the implementation of that method we have the sum the two values and return that.

Block C:

  • First we have defined the object of the delegate passing method as the parameter.
  • Then we will input two parameters from the user and save them in two variables.
  • Now we will activate the delegate using the object we created and save the result in another variable.
  • In the end we will show the result in the console.

Events

A hook on an object where the code outside of the object can say "When that something happens, that fires this event, please call my code".

Simple Example 2:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace @event

{

    public partial class Form1 : Form

     {

        public int a=1;

 

        public Form1()

        {

                  InitializeComponent();

                  this.button1.Click += new System.EventHandler(this.button1_Click);

                  this.button1.Click += new System.EventHandler(this.button1_Click);

 

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

           

             MessageBox.Show(a.ToString());

             a++;

        }

    }

}


Output:

OOP4.jpg

In the preceding example we have defined the simple event on button click. When we click the button once, it will display the message box three times or call the click event three times.

Delegate and Events

The best uses of delegates are in the events and we have daily use delegates with events without knowing them.

Simple Example 3:

OOP5.jpg

Output:

OOP6.jpg

Block A:

  • In this sessoion we have defined a delegate with the name OnEventHandler with two parameters.

Block B:

  • In this sessoion we have tagged the delegate with the event FiveEvent.
  • We have defined the method Gotfive. In that method we have activated the delegate using FiveEvent(this, EventArgs.Empty);
  • Another method callback having the same signature with delegates.

Block C:

  • In this sessoion we have defined an object of the class FEvent that is fevent.
  • Using that fevent we have instantiated the Delegate with the callback method.
  • Create the object of the Random class with the name of random.
  • We use a for loop and generate five numbers using the random object.
  • It will check the numbers; for each one that's five, it will activate the delegate.


Similar Articles