What is a delegate?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

What is a delegate?

Rating:

A delegate is a type of function pointer used by the .NET Framework. Technically, a delegate is used in function calling, events, and type-safety. The best feature of a delegate is that it does not know about the class of the object that it references at compile-time. The only thing that matters is that the argument(s) and return type of the method must match with the argument(s) and return type of the delegate. The .NET Framework has a System.Delegate namespace to handle delegates.

The following syntax is used to declare a delegate:

Modifier delegate-keyword return-type delegate-name(parameter lists); e.g.
public delegate void MyFirstDelegate(string name, int age);

The following code snippet displays the use of a delegate:

using System;
delegate void MyFirstDelegate(string str);
class MyFirstDelegateClass
{
   public static void Show(string str)
   {
      Console.WriteLine(" Show, {0}", str);
   }
   public static void Hide(string str)
   {
      Console.WriteLine(" Hide, {0}", str);
   }
   public static void Main()
   {
      MyFirstDelegate a1,b1;
      a1 = new MyFirstDelegate(Show);
      b1 = new MyFirstDelegate(Hide);
      Console.WriteLine("Call Delegate a:");
      a1("Del_A");// call Show()
      Console.WriteLine("Call Delegate b:");
      b1("Del_B");// call Hide()
   }
}


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.