Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2012
This is the forum to discuss the Wrox book Beginning Visual C# 2012 by Karli Watson, Jacob Vibe Hammer, Jon Reid, Morgan Skinner, Daniel Kemper, Christian Nagel, ; ISBN: 978-1-118-31441-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2012 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2014, 04:27 AM
Authorized User
 
Join Date: May 2014
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Question Why use delegate?

why use delegate, what's benefit of using delegate compare with using functions?
 
Old June 4th, 2014, 10:30 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

A delegate is a function and an essential component with event driven applications, without them you could not subscribe to events such as click events in windows forms and page_load in ASP.net.
 
Old June 5th, 2014, 09:58 PM
Authorized User
 
Join Date: May 2014
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Default

hi there,
what's difference between delegate and common function?
 
Old June 5th, 2014, 11:24 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

Quote:
Originally Posted by chinaNoob View Post
hi there,
what's difference between delegate and common function?
Really there is no difference, the delagate is a type that encapsulate a method, simalar to a function pointer in C if you are familar with those. it allows you pass functions around as types which is the basis of event handler which are delagates.

example

Code:
// delegate declaration
public delegate void DelegateHandler(string message);


// some method definition
public void MyHelloFunction(String message)
{
      Console.WriteLine("Hello from delagate");
}


// assing MyHelloFunction to the delegate; we can do this because the MyHelloFunction signiture matches the DelegateHandler signiture
DelegateHandler myHandler = MyHelloFunction; 

// call the hello function via the delegate
myHandler();

Last edited by mmorgan30; June 5th, 2014 at 11:38 PM..
 
Old June 9th, 2014, 02:39 AM
Authorized User
 
Join Date: May 2014
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I can understand what you described, but not the example code, cause my english is not good enough, and thanks! ^_^





Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 6, Why use delegate? chinaNoob BOOK: Beginning ASP.NET 4.5 : in C# and VB 2 June 4th, 2014 04:06 AM
Elaboration for delegate morteza BOOK: Beginning ASP.NET 4 : in C# and VB 7 June 6th, 2010 06:30 AM
custom delegate for ToolStripMenuItem maricar C# 4 September 4th, 2008 03:07 AM
Delegate query elwappo General .NET 6 June 5th, 2008 03:07 AM
What Is Delegate jayakumar.cj ASP.NET 1.0 and 1.1 Basics 1 July 15th, 2006 06:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.