Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
consider me a very beginner and please answer the q.

i headed that it is use to reference methods and events.
one thing is fine, that we invoke event whenever we want grammatically.
but what about methods?
i think there is some threading concept?
Asynchronous and Synchronous Mehtds?

please go step by step
Thank You so much
Posted

 
Share this answer
 
Comments
Monjurul Habib 10-Dec-11 1:41am    
nice links 5!
already answered:
What is delegate..? What is the use of it..?[^]

hope it helps :)
 
Share this answer
 
Comments
awaisdar 10-Dec-11 1:57am    
Asynchronous and Synchronous Methods.
1.What is the difference between the two? simple example.
thatraja 10-Dec-11 2:13am    
5!
Uday P.Singh 10-Dec-11 3:07am    
thank you Raja :)
Monjurul Habib 11-Dec-11 20:56pm    
5!
Uday P.Singh 12-Dec-11 4:19am    
thank you Monjurul :)
Delegates are not just related to threading, you can use them at any time - and do, although you don't always realise it.

A delegate is a method reference (what would be called a function pointer in other languages) - it allows you to set up a method to be called at a later date. When you call the method, you do not need to know what it is called - you just execute it from the delegate. It is the whole mechanism on which events work!

Why do we use them? Because they can be faster (you don't need to do "if this then that, else theOther", you can so go DoIt and run the preset method). Because they can make methods more generic: a delegate with can be loaded with as Console or TextBox write method can be called throughout a method, or class, and the same code handles both functions.


"it is said they at design time you dont know the function name, so you use delegate.?

even then when you create delegate to point to all the methods that satisfies some catagory you do it by

public delegate void Name (string a);

rite here it is pointing all the mmethods that have same signate like this one.

but when you execute it?
Name object = new object(method name);

here you are spacifing the name of the method?
rite?
so as i said we dont know the name of the method than how could we?
we could not execute it withot its name referenced.
please explian."



A delegate does not point at all teh methods, it doesn't point at anything until you give it a value. In the same way that to use a class variable, you must tell it which instance of the class it refers to, you have to tell the delegate which method it refers to:

C#
private delegate int MyDelegate(string s);
private MyDelegate execute = null;
private int total = 0;
private void ShowDelegate()
    {
    execute = UseNameSmith;
    DoDelegate();
    execute = UseNameJones;
    DoDelegate();
    Console.WriteLine(total);
    }
private void DoDelegate()
    {
    if (execute != null)
        {
        total += execute("Hello");
        }
    }
private int UseNameSmith(string s)
    {
    Console.WriteLine(s + " Smith");
    return 1;
    }
private int UseNameJones(string s)
    {
    Console.WriteLine(s + " Jones");
    return 2;
    }

The first line declares the delegate - it defines what method signature it can accept. If you try to assign a method that has different parameters, it will complain!
The second declares the delegate variable - this is where you store the method instance you want to call.
The rest just shows what happens if you use a delegate.

The important thing to notice is that the DoDelegate method does not change - it is tested, working code, so we do not have to alter it in any way to get similar but different results. This is one of the big advantages of delegates: flexibility combined with reliability. Provided the methods you assign to the delegate variable do what they are supposed to, the original, working code does not need to be changed to provide increased functionality.

Because delegates are method references, you can do some clever things with them, that improve performance. For example, if you wanted to execute a different method based on what enum value was passed to a method, you could go:
C#
if (value == myEnum.firstOption)
   executeMethodOne();
else if (value == myEnum.secondOption)
   exeecuteMethodTwo();
else ...
But that is a lot of code, and a lot of tests!
You could do it with a switch:
C#
switch (value)
    {
    default:
        break;
    case myEnum.firstOption:
        executeMethodOne();
        break;
    case myEnum.secondOption:
        exeecuteMethodTwo();
        break;
    ...
    }
Or, if you set up your enum correctly (and do some sensible bounds checking) you could set up an array of delegates and do it in one line:
C#
private MyDelegate[] myTable = new MyDelegate[] ;
    ....
    myTable[value]("Goodbye");
Ok, you have to set up the methods in the class constructor, or use static methods only to set them up at compile time, but you save time when you execute it, and that is generally more important.

(That, by the way, is an oversimplification - the compiler does pretty much that with a well designed switch anyway)
 
Share this answer
 
v2
Comments
Monjurul Habib 10-Dec-11 1:41am    
very nice answer,5!
thatraja 10-Dec-11 2:00am    
I think you have big time always, Only freelancers.
5!
Monjurul Habib 11-Dec-11 20:57pm    
:)
i have some ques.
i have understood the concept of handling the event using delegates (as some extent not whole). but there is some thing else here.<b>
Asynchronous and Synchronous Methods.
1.What is the difference between the two? simple example.</b>

<b>2. i have a method that takes 30 mint to complete, i dont want it to hangup the system, i want it to go to the background and let me use my system meanwhile. and when it finishes the execution, it informs me that it has completed and returns (if any) data that it supposed to do. How will i implement it C#, Using Threading and Delegates? </b>

Consider me the beginner in programming and explain it upto my level.
thank you so much.
i will appreciate it.
a little example wud make it easy for me
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900