Share via

Raise and Consume an Event in C#

Shervan360 1,681 Reputation points
2022-07-20T11:25:07.333+00:00

Hello,

Please see the example 2 in the below link:
https://learn.microsoft.com/en-us/dotnet/standard/events/how-to-raise-and-consume-events

We can rewrite this example and remove the OnThresholdReached method.

I rewrite example 2, without that method and works fine.

public class Counter  
{  
    private int threshold;  
    private int total;  
    public Counter(int passedThreshold)  
    {  
        threshold = passedThreshold;  
    }  
    public void Add(int _value)  
    {  
        total += _value;  
        if (total >= threshold)  
        {  
            ThresholdReachedEventsArgs args = new();  
            args.Threshold = total;  
            args.TimeReached = DateTime.Now;  
            handler(this, args);  
        }  
    }  
    public event EventHandler<ThresholdReachedEventsArgs> handler;  
}  
  
public class ThresholdReachedEventsArgs : EventArgs  
{  
    public int Threshold { get; set; }  
    public DateTime TimeReached { get; set; }  
}  

I have three question here:

1- Why should we write OnThresholdReached method?
2- Why this method should be protected?
3- Why this method should be vitrual?

Thank you

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-07-20T15:56:57.137+00:00

    Why should we write OnThresholdReached method?

    You do not have to use a method, see example below

    Why this method should be protected?

    You don't have too unless there is a business rule, see the docs

    Why this method should be vitrual?

    Same as protected see the docs

    Run the code https://dotnetfiddle.net/RM5QNj (I tried to post the code but got messed up)

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Reza Aghaei 4,996 Reputation points Volunteer Moderator
    2022-07-20T16:54:29.313+00:00

    It’s not compulsory to have OnXXXX method but the document is sharing some best practices.

    There should be a method and it should be virtual to allow overriding and allow future customisation. And respecting to open-closed principle.

    It should be protected so that the deriving class has access to it and could override it ir raise the event, but other classes not have any access to it.

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 83,816 Reputation points
    2022-07-20T15:13:11.01+00:00

    if you are creating a class to be inherited, then it makes sense to supply a method that is called as part of the event invoke. protected means it can only be called by a sub class, and virtual means it can be overridden (the point of the method). a subclass can override this method, and add its own processing.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.