C#  

Understanding C# Events Using a Simple ICU Example

📌 Introduction

Events are a core concept in C# that enable different parts of an application to communicate cleanly and loosely coupled.

Instead of one class directly calling methods on another, it can simply raise an event and let interested components respond.

In this article, we will understand C# events using a very simple real-life example:
A patient entering an ICU and hospital staff being notified.

Note: This ICU room example is used purely to explain the concept of events in a simple and relatable way. It is not intended to represent real-world medical systems or production-level healthcare software.

No frameworks. No advanced patterns. Just clear fundamentals.

❓ What Is an Event in C#?

An event is a mechanism that allows a class to notify other classes when an event occurs.

In simple terms:

  • One class raises an event

  • Other classes subscribe to the event

  • The publisher does not know who is listening

This makes the code easier to maintain and extend.

🏥 Real-Life Scenario: ICU Room

When a patient enters the ICU:

  • The doctor should be informed

  • The nurses should be informed

Important Point

  • The ICU room should not directly call doctor or nurse methods

  • Instead, it raises an event, and whoever is interested can react

💻 Simple Example Code

// Delegate defines what methods can handle the event
public delegate void ICURoom();

public class Program
{
    // Event declaration
    public event ICURoom? InformStaffAboutPatientHasEnteredInICU;

    // Event subscribers
    public void InformDoctor()
    {
        Console.WriteLine("Informing Doctor About The Patient");
    }

    public void InformNurse()
    {
        Console.WriteLine("Informing Nurses About The Patient");
    }

    // Method that raises the event
    public void PatientEnteredInICU()
    {
        Console.WriteLine("Patient has entered the ICU");
        InformStaffAboutPatientHasEnteredInICU?.Invoke();
    }

    static void Main(string[] args)
    {
        Program program = new Program();

        // Subscribing to the event
        program.InformStaffAboutPatientHasEnteredInICU += program.InformDoctor;
        program.InformStaffAboutPatientHasEnteredInICU += program.InformNurse;

        // Triggering the event
        program.PatientEnteredInICU();

        Console.ReadLine();
    }
}

🧩 Step-by-Step Explanation

1️⃣ Delegate

public delegate void ICURoom();

The delegate defines the method signature that can respond to the event.

2️⃣ Event Declaration

public event ICURoom? InformStaffAboutPatientHasEnteredInICU;

This event represents:
"A patient has entered the ICU."

3️⃣ Subscribing to the Event

program.InformStaffAboutPatientHasEnteredInICU += program.InformDoctor;
program.InformStaffAboutPatientHasEnteredInICU += program.InformNurse;
  • Doctor subscribes to the event

  • Nurse subscribes to the event

  • Multiple subscribers can listen to the same event

4️⃣ Raising the Event

InformStaffAboutPatientHasEnteredInICU?.Invoke();
  • The event is raised

  • All subscribed methods execute automatically

  • ?. prevents runtime errors if no subscribers exist

📤 Output

Patient has entered the ICU
Informing Doctor About The Patient
Informing Nurses About The Patient

🤔 Why Use Events Here?

  • The ICU room does not depend on doctor or nurse logic

  • New staff can be added without changing ICU code

  • Code remains clean, flexible, and maintainable

This is exactly what events are designed for.

🎯 Simple Interview Explanation

Events allow one class to notify multiple other classes about an action without knowing who those classes are.

Clear, short, and effective.

🧠 Conclusion

This simple ICU example demonstrates how events work in C# at a fundamental level.

The same concept is used in real applications such as:

  • Button clicks

  • Notifications

  • Background tasks

Once this foundation is clear, learning advanced event patterns becomes much easier.

🙏 Thank You

Thank you for reading this article.

I hope this simple ICU example helped you understand events in C# clearly.

Feel free to share your feedback or questions in the comments section.