This is a C# Program to demonstrate trigger concept.
This C# Program Demonstrates Trigger Concept.
Here the trigger concept is used and the numbers are added and displayed.
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(); } }
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.
Counter Overflow hval = 150
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Buy MCA Books
- Apply for C# Internship
- Buy Computer Science Books
- Buy C# Books