C# Program to Demonstrate Trigger

This is a C# Program to demonstrate trigger concept.

Problem Description

This C# Program Demonstrates Trigger Concept.

Problem Solution

Here the trigger concept is used and the numbers are added and displayed.

Program/Source Code

Here is source code of the C# Program to Demonstrate Trigger Concept. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate Trigger Concept
 */
using System;
delegate bool Condition(object obj);
delegate void Action(object obj);
class Counter
{
    int val = 0;
 
    public event Condition cond;
    public event Action evn;
 
    public int Value { get { return val; } }
 
    public void addition(int x)
    {
        val += x; Checkpoint();
    }
 
    public void Clearall()
    {
        val = 0; Checkpoint();
    }
 
    void Checkpoint()
    {
        if (cond != null && evn != null && cond(this)) evn(this);
    }
}
class Test
{
    static int hval = 0;
    static bool CheckpointLimit(object ctr)
    {
        return (((Counter)ctr).Value > 100);
    }
    static void Alarm(object ctr)
    {
        Console.WriteLine("Counter Overflow");
    }
    static void Reset(object ctr)
    {
        hval = ((Counter)ctr).Value;
        Console.WriteLine("hval = " + hval);
        ((Counter)ctr).Clearall();
    }
    public static void Main()
    {
        Counter counter = new Counter();
        counter.cond += new Condition(CheckpointLimit);
        counter.evn += new Action(Alarm);
        counter.evn += new Action(Reset);
        counter.addition(10);
        counter.addition(20);
        counter.addition(30);
        counter.addition(40);
        counter.addition(50);
        Console.Read();
    }
}
Program Explanation

This C# program is used to demonstrate trigger concept. Triggers and Actions model are cause-and-effect relationships. A Trigger reacts to the cause and invokes one or more Actions. A Trigger is an object that listens for a specific condition.

advertisement

Such as firing an event or a property being set to a certain value, and invokes one or more associated Actions in response. A Trigger is an object that can only listen for something to happen. An Action is an object that can only do something. Perform the action() function by passing Alarm and Reset function value as an argument.

In Alarm() function it will execute the statement an print the counter overflow. In Reset() function ‘hval’ variable is used to display the counted values. Here the trigger concept is used and the numbers are added and displayed.

Runtime Test Cases
 
Counter Overflow
hval = 150

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

🎓 Free Certifications on 300 subjects are live for December 2025. Register Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.