What is an Event?

Started by beingchinmay, 11-23-2016, 05:34:49

Previous topic - Next topic

beingchinmayTopic starter

•   An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object.

•   An event is a way for a class to allow clients to give it delegates to methods that should be called when the event occurs. The most familiar use for events is in graphical user interfaces.

•   Event Handlers in the .NET Fram  work r  turn void and take two parameters.

•   The first parameter is the source of the   v nt; that is the publishing object.

•   The second parameter is an object   erived from EventArgs.

•   Events are properties of the class publishing the event.

•   The keyword event controls how the event property is accessed by the subscribing classes.


damponting44

#1
In C#, an event is a way for a class to provide notifications to other classes (referred to as subscribers or listeners) when something interesting happens to an object. It allows the subscribers to register their interest in receiving these notifications and perform certain actions when the event occurs.

Events are commonly used in programming scenarios where one object needs to communicate with multiple objects without having direct knowledge of those objects. For example, in graphical user interfaces, events are used to handle user interactions such as button clicks, mouse movements, or keyboard input.

By defining and raising events, a class can encapsulate the occurrence of specific actions and allow other classes to respond accordingly. This helps in establishing loose coupling between classes, promoting better code organization, reusability, and maintainability.

additional details about events in C#:

1. Event Declaration: Events are declared using the event keyword followed by the delegate type that defines the signature of the methods that can be subscribed to the event. For example:
   ```
   public event EventHandler MyEvent;
   ```

2. Subscribing to an Event: To subscribe to an event, a method with a compatible signature should be assigned to the event. This can be done using the += operator. For example:
   ```
   myObject.MyEvent += EventHandlerMethod;
   ```

3. Event Handler Method: An event handler method is a method that matches the signature of the delegate type associated with the event. It typically takes two parameters - the source of the event (the publishing object) and an object derived from EventArgs that provides additional information about the event. For example:
   ```
   void EventHandlerMethod(object sender, EventArgs e) { ... }
   ```

4. Raising an Event: To notify subscribers about an occurrence of an event, the event can be raised within the class using the event name. This invokes all the subscribed event handler methods. For example:
   ```
   MyEvent?.Invoke(this, EventArgs.Empty);
   ```

5. Unsubscribing from an Event: To unsubscribe from an event, the -= operator is used to remove the assigned method from the event. For example:
   ```
   myObject.MyEvent -= EventHandlerMethod;
   ```

6. Custom Event Arguments: If additional data needs to be passed with the event, a custom event arguments class can be created by deriving from the EventArgs class. This allows for passing specific data relevant to the event. For example:
   ```
   public class CustomEventArgs : EventArgs { ... }
   public event EventHandler<CustomEventArgs> MyEvent;
   ```

few more details about events in C#:

1. Multicast Events: Events in C# are multicast, which means that multiple event handlers can be subscribed to an event. When the event is raised, all the subscribed event handlers will be called sequentially. This allows multiple subscribers to respond to the same event.

2. Event Access Modifiers: Events can have different access modifiers such as public, private, protected, or internal, just like other class members. The choice of access modifier determines which classes can subscribe to the event.

3. Null-Conditional Operator: The null-conditional operator (?.) is often used when raising the event to ensure that the event is only raised if there are registered event handlers. It prevents a null reference exception if there are no subscribers to the event. For example:
   ```
   MyEvent?.Invoke(this, EventArgs.Empty);
   ```

4. Custom Event Invocator Method: Instead of directly using the MyEvent?.Invoke() syntax, it is common to encapsulate the event raising logic in a separate method called the event invocator. By convention, this method is typically named OnEventName, where EventName represents the name of the event. For example:
   ```
   protected virtual void OnMyEvent(EventArgs e)
   {
       MyEvent?.Invoke(this, e);
   }
   ```

5. Async Events: Starting with C# 7.0 and later versions, events can also be declared as async. This allows for asynchronous event handlers, which can be useful when performing long-running or IO-bound operations. The EventHandler delegate can be replaced with the EventHandler<T> delegate, where T represents the custom event arguments.


event is an action that occurs as a result of the user or another source, such as a mouse being clicked, or a key being pressed.
Example of an Event:
A file being created or modified on a filesystem.
A hardware sensor such as a webcam or microphone receiving sensory input.


johnv5

An event is an action or occurrence such as a mouse click, a key press, mouse movements, or any system-generated notification. A process communicates through events. For example, interrupts are system-generated events. When events occur, the application should be able to respond to it and manage it. Events in ASP.NET raised at the client machine, and handled at the server machine. For example, a user clicks a button displayed in the browser. A Click event is raised. The browser handles this client-side event by posting it to the server. The server has a subroutine describing what to do when the event is raised; it is called the event-handler. Therefore, when the event message is transmitted to the server, it checks whether the Click event has an associated event handler. If it has, the event handler is executed.
  •