C# program to terminate the current running program explicitly

Here, we are going to learn how to terminate the current running program explicitly in C#?
Submitted by Nidhi, on September 10, 2020

Here we will read the value on an integer number, if the value of the number is greater than 5 then we terminate the program using the Exit() method of Environment class.

Program:

The source code to terminate the current running program is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to terminate the current running program.
using System;

class Demo
{
    public static void Main()
    {
        int num = 0;

        try
        {
            Console.Write("Enter the value of num: ");
            num = int.Parse(Console.ReadLine());

            if (num > 5)
            {
                Console.WriteLine("Program terminated explicitly");
                Environment.Exit(0);
            }
        }
        finally
        {
            Console.WriteLine("Finally block executed");
        }
    }
}

Output:

Enter the value of num: 7
Program terminated explicitly
Press any key to continue . . .

Explanation:

In the above program, we created a Demo class that contains the Main() method. In the Main() method we read the value of variable num and then check the value of num. If the value of num is greater than 5 then we print the message "Program terminated explicitly" and terminated the program using the Exit() method of Environment class. If the value of variable num is less than or equal to 5 then the "Finally block executed" message will be printed on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.