Se afișează postările cu eticheta delegate. Afișați toate postările
Se afișează postările cu eticheta delegate. Afișați toate postările

luni, 25 august 2014

Delegates in c#

A delegate in C# is similar to a function pointer in C++. This allows encapsulating a reference to a method inside a delegate object.
A delegate is a type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance.

An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references
Delegates are especially used for implementing events and the call-back methods.

1. Basic delegate example

// declaration

        private delegate void TestDelegate();

        private  void StartTestDelegate()
        {
            MessageBox.Show("StartTestDelegate");  
        }
        private void StopTestDelegate()
        {
            MessageBox.Show("StopTestDelegate");
        }

// Instantiation
        TestDelegate del1 = new TestDelegate(StartTestDelegate);
        TestDelegate del2 = new TestDelegate(StopTestDelegate);

// Invocation
        del1();
        del2();

2. Math Operator delegate example

  private class Math
  {
            public int Add(int x, int y)
            {
                return x + y;
            }
            public int Sub(int x, int y)
            {
                return x - y;
            }
            public MathDelegate Operator(int op)  
            {
                MathDelegate objDelegateMath = null;  // delegate reference
                if (op == 1)
                    objDelegateMath = Add;  // point the reference to methods
                else
                    objDelegateMath = Sub;  // point the reference to methods
               return objDelegateMath;  
            } 
  }

  Math objMath = new Math();
  //Invoke the methods through delegate
  MessageBox.Show(objMath.Operator(1).Invoke(1, 2).ToString());
  MessageBox.Show(objMath.Operator(2).Invoke(5, 4).ToString());  

3. Logger class delegate example 

Multicast delegates >>

Mulicast delegate in C#

Mulicast delegate is a delegate that can have more than one element in its invocation list.

Example:

public delegate void MultiDelegate(int x,int y);
private class Operator
{
            public void Add(int x, int y)
       {
              MessageBox.Show ("The sum is: " + (x + y));
       }
       public void Sub(int x, int y)
       {
              MessageBox.Show("The sub is: " + (x - y));
       }
}

MultiDelegate ptr=null;
//add more than one function called multicast delegate.
ptr += new MultiDelegate(obj.Add);
ptr += new MultiDelegate(obj.Sub);
ptr(50, 10);

<< Delegates                                                                                  Anonymous functions >>

Predicate Delegate C# Example

Predicate<T> Delegate

This delegate is used to search for elements in the collection.
Predicate<T> delegate is represented by a lambda expression. 

Example

public class Student
{
       public string Name { get; set; }
       public int Year { get; set; }
       public Student(string name, int year)
       {
              this.Name = name;
              this.Year  = year;
       }
}

List<Student> groups = new List<Student>();
groups.AddRange(
                       new Student[] 
                                { 
                                new Student("Stud1", 1973), 
                                new Student("Stud2", 2000),
                                new Student("Stud3", 2010),
                                new Student("Stud4", 1999),
                                new Student("Stud5", 1995) 

                                }
                       );

foreach (var g in groups.FindAll(x => x.Year >= 2000))
              System.Windows.MessageBox.Show(g.Name);