IT Community - Software Programming, Web Development and Technical Support

Delegates

This is a discussion on Delegates within the C# Programming forums, part of the Software Development category; What mean by delegates,how import or define in C# Thanks, Prasath.K...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-16-2007, 06:04 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Question Delegates

What mean by delegates,how import or define in C#



Thanks,
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-19-2007, 05:14 AM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Re: Delegates

A delegate is a reference type that refers to a Shared method of a type or to an instance method of an object.

Delegate is like a function pointer in C and C++.

Delegate lets some other code call your function without needing to know where your function is actually located.

Events are really a modified form of a delegate.

Example Program:


Code:
using System;
class clsDelegate
{	
	public delegate int simpleDelegate (int a, int b);
	public int add(int a, int b)	
	{		
		return (a+b);
	}	

	public int mul(int a, int b)	
	{		
		return (a*b);	
	}	
	static void Main(string[] args)	
	{		
		clsDelegate clsDlg = new clsDelegate();		
		simpleDelegate add = new simpleDelegate(clsDlg.add);
		simpleDelegate mul = new simpleDelegate(clsDlg.mul);		
		int addAns = add(10,12);		
		int mulAns = mul(10,10);		
		Console.WriteLine("Result by calling the add method using a delegate: {0}",addAns);		
		Console.WriteLine("Result by calling the mul method using a delegate: {0}",mulAns);		
		Console.Read();	
	}
}
Note : Delegates should have the same signature as the methods to be called by them.

Last edited by Gopisoft : 07-19-2007 at 05:22 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-20-2007, 07:55 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: Delegates

An interesting feature in C# is Delegate. Delegates are best complemented as new type of Object in C#. They are also represented as pointer to functions. Technically delegate is a reference type used to encapsulate a method with a specific signature and return type.


There are two types of delegates available in C#.

1. Single Delegate
2. Multi-cast Delegate

Here is the example of delegate as follows,

Code:
using System;
namespace testWinApp
{

// 1. Define delegate.

public delegate double UnitConversion(double from);

public partial class Form1 : Form
   	 {
       	 public Form1()
       	 {
        	    InitializeComponent();
        	 }
		
		// 2. Define handler method.

       	 public static double FeetToInches(double feet)
        	 {
         	   return feet * 12;
       	 }

private void button4_Click(object sender, EventArgs e)
        	{

		// 3. Create delegate instance.

            UnitConversion doConversion = new 
							UnitConversion(FeetToInches);

		// 4. Use delegate just like a method.

            double inch = doConversion(4);
            MessageBox.Show(inch.ToString());
        }
	}
}
Result will be 48.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 03:53 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0