Delegates in C# – Part 2

To understand C# delegates I would suggest you to refer previous post Delegates in C# – Part 1 as this post talks only about how to create delegate using different ways.

Class Student: This class is used in subsequent examples to understand different ways of delegate creation.

To understand each example please refer comments under respective sections, without writing further let us jump to code snippets.

public class Student
{
public Student(int rollNo, string name, int marks)
{
RollNo = rollNo;
Name = name;
Marks = marks;
}

public int RollNo { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}

Method 1:

//This is old way of creating delegate (C# 1.1)
//Below are the steps:
//1. We declare delegate type - CheckMarksLessThan80_Delegate
//2. We write a method with same signature as the signature of delegate - CheckMarksLessThan80_function
//3. We create an instance of delegate - mydelegate
//4. Make a method call using delegate instance

//Step 1
public delegate bool CheckMarksLessThan80_Delegate(Student student);

//Step 2
public bool CheckMarksLessThan80_function(Student student)
{
return student.Marks < 80;
}

public void TestDelegateCreationMethod1()
{
bool marksLessThan80 = false;

Student student = new Student(1, "Jack", 70);

//Step 3
CheckMarksLessThan80_Delegate mydelegate =
new CheckMarksLessThan80_Delegate(CheckMarksLessThan80_function);

//Step 4
marksLessThan80 = mydelegate(student);

Console.WriteLine("Method 1: Marks less than 80: {0}", marksLessThan80);
}

Method 2:

// Creating delegate using delegate keyword (C# 2.0)
//Below are the steps:
//1. We declare delegate type - CheckMarksLessThan80_Delegate
//No need to write a method with same signature as the signature of delegate
//2. Create instance of delegate using "delegate" keyword
//3. Make a method call using delegate instance

//Step 1
public delegate bool CheckMarksLessThan80_Delegate(Student student);

public void TestDelegateCreationMethod2()
{
bool marksLessThan80 = false;
Student student = new Student(1, "Jack", 60);

//Step 2
CheckMarksLessThan80_Delegate mydelegate =
delegate (Student s) { return s.Marks < 80; };

//Step 3
marksLessThan80 = mydelegate(student);

Console.WriteLine("Method 2: Marks less than 80: {0}", marksLessThan80);
}

Method 3:

//Creating delegate using lambda expression (C# 3.0)
//Below are the steps:
//1. We declare delegate type - CheckMarksLessThan80_Delegate
//No need to write a method with same signature as the signature of delegate
//2. Create instance of delegate using using lambda expression
//3. Make a method call using delegate instance

//Step 1
public delegate bool CheckMarksLessThan80_Delegate(Student student);

public void TestDelegateCreationMethod3()
{
bool marksLessThan80 = false;
Student student = new Student(1, "Jack", 95);

//Step 2
CheckMarksLessThan80_Delegate mydelegate = s => { return student.Marks < 80; };

//Step 3
marksLessThan80 = mydelegate(student);

Console.WriteLine("Method 3: Marks less than 80: {0}", marksLessThan80);
}

Method 4:

//Use .net provided delegate Predicate<in T>
//Below are the steps:
//No need to declare your delegate
//No need to write function as we can use lambda expression or anonymous method with delegate keyword
//1. Create instance of delegate "Predicate"
//2. Make a method call using delegate instance
public void TestDelegateCreationMethod4()
{
bool marksLessThan80 = false;
Student student = new Student(1, "Jack", 90);

//Step 1 - Anonymous methodusing lambda expression
Predicate<Student> mydelegate = s => { return student.Marks < 80; };
//Step 1 - Anonymous method using delegate
//Predicate<Student> mydelegate = delegate (Student s) { return s.Marks < 80; };

//Step 2
marksLessThan80 = mydelegate(student);

Console.WriteLine("Method 4: Marks less than 80: {0}", marksLessThan80);
}

Note: Predicate is not the only helper delegate provided by .NET, there are few more delegates provided by .NET – Func, Action, Converter, Comparison

One thought on “Delegates in C# – Part 2

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s