Computer Science, asked by shivakumaryadav30641, 1 year ago

What are events in C#?

Answers

Answered by iiiii12
0

Declaring Events

To declare an event inside a class, first a delegate type for the event must be declared. For example,

public delegate string MyDel(string str);

Next, the event itself is declared, using the event keyword −

event MyDel MyEvent;

The preceding code defines a delegate named BoilerLogHandler and an event named BoilerEventLog, which invokes the delegate when it is raised.

Example

using System;

namespace SampleApp {

public delegate string MyDel(string str);

class EventProgram {

event MyDel MyEvent;

public EventProgram() {

this.MyEvent += new MyDel(this.WelcomeUser);

}

public string WelcomeUser(string username) {

return "Welcome " + username;

}

static void Main(string[] args) {

EventProgram obj1 = new EventProgram();

string result = obj1.MyEvent("Tutorials Point");

Console.WriteLine(result);

}

}

Similar questions