We help IT Professionals succeed at work.

how to create event manually in C#?

jazzIIIlove
jazzIIIlove asked
on
Hi there;

I want to create event and call it, just to learn the idea, and relation with delegates.

How to?

Regards.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Delegates are basically are well known as user controlled events. Using delegates programmer can give call to a specific code block. Here is a sample to show;

Create Delegate and it's respective event pair.
 public delegate int GetNumber();
        public event GetNumber RequestForNumbers;

Open in new window


Add event handler which will serve the request
 this.RequestForNumbers += new GetNumber(ReturnNumber);

Open in new window


Call the delegate method
            int n = RequestForNumbers();

Open in new window


Method which will be called on delegate,
int ReturnNumber()
        {
            return 20;
        }

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.