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);
    public static void Main()
        Counter counter = new Counter();
        counter.cond += new Condition(CheckpointLimit);
        counter.evn += new Action(Alarm);
        counter.evn += new Action(Reset);
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.


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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.