top

Search

C# Tutorial

Events in C# are actions that allow classes or objects to inform other classes or objects when an interesting phenomenon occurs.Some of the properties of events are given as follows:Events have publishers and subscribers. The publishers determine when an event is raised while the subscribers determine the actions that are performed in response to the event.If there are no subscribers for an event, then event is never raised. However, for an event there can be many subscribers.If an event has many subscribers, when the event is raised the event handlers are invoked synchronously.User actions such as menu selections, button clicks etc. are signaled by using events.The events are based on the EventHandler delegate and the EventArgs base class.Event SyntaxThe events are declared using the keyword event. The syntax for this is given as followed:event delegate_name event_name;In the above syntax, the keyword event is followed by the delegate name and then the event name.A program that demonstrates events is given as follows:Source Code: Program to implement Events in C#using System; namespace Example {   public delegate string demoDelegate(string str1, string str2);   class MyEvents {      event demoDelegate myEvent;      public MyEvents() {         this.myEvent += new demoDelegate(this.Display);      }      public string Display(string studentname, string subject) {         return "Student: " + studentname + "\nSubject: " +subject;      }      static void Main(string[] args) {         MyEvents e = new MyEvents();         string res = e.myEvent("Jack", "Science");         Console.WriteLine("RESULT...\n"+res);      }   } }The output of the above program is as follows:RESULT... Student: Jack Subject: Science
logo

C# Tutorial

Events in C#

Events in C# are actions that allow classes or objects to inform other classes or objects when an interesting phenomenon occurs.

Some of the properties of events are given as follows:

  1. Events have publishers and subscribers. The publishers determine when an event is raised while the subscribers determine the actions that are performed in response to the event.
  2. If there are no subscribers for an event, then event is never raised. However, for an event there can be many subscribers.
  3. If an event has many subscribers, when the event is raised the event handlers are invoked synchronously.
  4. User actions such as menu selections, button clicks etc. are signaled by using events.
  5. The events are based on the EventHandler delegate and the EventArgs base class.

Event Syntax

The events are declared using the keyword event. The syntax for this is given as followed:

event delegate_name event_name;

In the above syntax, the keyword event is followed by the delegate name and then the event name.

A program that demonstrates events is given as follows:

Source Code: Program to implement Events in C#

using System;
namespace Example {
  public delegate string demoDelegate(string str1, string str2);
  class MyEvents {
     event demoDelegate myEvent;
     public MyEvents() {
        this.myEvent += new demoDelegate(this.Display);
     }
     public string Display(string studentname, string subject) {
        return "Student: " + studentname + "\nSubject: " +subject;
     }
     static void Main(string[] args) {
        MyEvents e = new MyEvents();
        string res = e.myEvent("Jack", "Science");
        Console.WriteLine("RESULT...\n"+res);
     }
  }
}

The output of the above program is as follows:

RESULT...
Student: Jack
Subject: Science

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial