473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

c# code

hi
i was going thru some samples and i couldnt understand what this line means

public delegate void DataListItemEventHandler(object sender,
DataListItemEventArgs args);
what is a delegate and what are delegates used for?
thnx
Nov 16 '05 #1
2 1252
A delegate is like a strongly-typed function pointer :). Did that help?

Basically, it's a way you can treat code as data -- store it, pass it
around, and call it when you want.

Every event takes a delegate, for instance:

private myMethod(object sender, EventArgs e) {
MessageBox.Show("hi!");
}

// in some other place:
myEventHandler = new EventHandler(myMethod);
someButton.Click += myEventHandler;

In this case, we create a new EventHandler delegate, and attach it to the
Click event. Then, when the event is raised, it invokes all the delegates
that have been attached.

Another use would be for benchmarking. For a benchmark, I might want to find
out how long method X and Y take. The basic code would be:

long start = QueryPerformanceCounter();
doSomeCode(iterations);
long end = QueryPerformanceCounter();
long time = end-start;
Console.WriteLine("SomeMethod took {0} seconds.", time /
QueryPerformanceFrequency());

And it might have other things like garbage collection and so on. If I want
to test 10 methods, I need to replicate the code 10 times. With a delegate,
I could make that code into a method that takes a delegate.

public delegate void BenchmarkDelegate(int iterations);

public doBenchmark(BenchmarkDelgate methodToTest) {
// timing code
methodToTest(iterations);
// timing code
}

-mike
MVP

"juan" <ic***@hotmail.com> wrote in message
news:ec*************@TK2MSFTNGP12.phx.gbl...
hi
i was going thru some samples and i couldnt understand what this line
means

public delegate void DataListItemEventHandler(object sender,
DataListItemEventArgs args);
what is a delegate and what are delegates used for?
thnx


Nov 16 '05 #2
The concept of delegates is similar to the function pointers used in C++.
You may want to look at examples at:
http://www.programmersheaven.com/2/Les_CSharp_10_p1
However, if you have Java background, may be more familar source would be:
http://www.onjava.com/pub/a/onjava/2...delegates.html

Fakher Halim
Software Architect

"juan" <ic***@hotmail.com> wrote in message
news:ec*************@TK2MSFTNGP12.phx.gbl...
hi
i was going thru some samples and i couldnt understand what this line means
public delegate void DataListItemEventHandler(object sender,
DataListItemEventArgs args);
what is a delegate and what are delegates used for?
thnx

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
37
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.