Click here to Skip to main content
15,923,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
c# has two types of members--static and instance members
For invoking static members using name of the class
For invoking instance members using instance of the class
but why do u need delegates in c# for invoking members




please help me.

thank u

What I have tried:

For invoking static members using name of the class
For invoking instance members using instance of the class
but why do u need delegates in c# for invoking members
Posted
Updated 15-Nov-17 20:51pm

Delegates are effectively "pointers to functions" - so they let you store a "function" in a variable and execute it at a later time.

This is used for event handlers: A button is clicked, which raises a Click event.
The system looks at the Click event handler variable, and executes the function it finds in there. (That's a horrible simplification but it'll do for now.)

This also allows you to pass a function to a method at run time, and that's very handy.
For example, you can write a method that prints a document - and pass it the function that actually prints to the screen at run time, or the function that prints to the printer, or a file, or a database, or ... all without having to change in any way the method that decides what to print where on a page - it doesn't need to know what it is printing to, it just calls the appropriate function from the delegate you passed it.
 
Share this answer
 
Comments
phil.o 15-Nov-17 18:16pm    
My 5 :)
Not to mention that delegates do not have anything to do with static or instance members, specifically. A delegate can be static or not.
(I mention that, not for you, but for OP who seems to mix several unrelated things).
Your question leads me to believe you need to get a more solid basis for understanding C# and .NET fundamentals. I suggest you get a good book, and study.

Here are two free books: [^], [^]

Static Types in C#, defined in a Class, can be thought of as "instances of the Class," rather than instances of members of a Class instance. Static members are inherently singleton objects: no matter how many instances of a Class are created, there will be only one of each static member.
 
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