Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

C# Console Based Application




C# Console based application

Console programs are easy to develop because console-based applications perform all their input and output at the command line; they are ideal for quickly trying out language features and writing command-line utilities. If you have ever learned a programming language, you know that they all start with the “Hello, world!” program. Here also we start with the “Hello, world!”.

Hello World – Your First Program

In order to create a C# console based application

  1. Open your Visual Studio
  2. On the File menu, click New Project.
    Then the New Project dialog box appears. This dialog box lists the different default application types.
  3. Select Console Application as your project type and change the name of your application at the bottom textbox.
    If you are npt comfortable with default location, you can always enter a new path if you want.
  4. Then Click OK.
    After click OK button, you will get a screen like the following picture.

Here in the following program, we print a “Hello, world!” message only. So copy and paste the following command in the main code block.

Console.WriteLine("Hello, world!");

Here you can see the full source code.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
        }
    }
}

After enter the command, the next step is to run the program. You can run your program using Ctrl+F5 . Then Visual Studio will keep the console window open, until you press a key. You will get screen look like the following picture.

Now you created your first program in Visual Studio.

How to stop C# console applications of closing automatically

The first program is to run your application by using Ctrl+F5, so the console window will remain open until you press a key. The disadvantage of this (Ctrl+F5) is that you lose Visual Studio’s debug information. If you run your console application by using F5 instead of Ctrl+F5, you can see the window will disappear suddenly. It is because your program is finished. When console applications have completed executing and return from their main method, the associated console window automatically closes. This is the expected behaviour.

Prevent a C# Console Application from closing when it finishes

If you want to keep the application open for debugging purposes, you’ll need to instruct the computer to wait for a keypress before ending the app and closing the window. In this case, you can use Console.ReadLine() to wait for the user to Enter or Console.ReadKey to wait for any key.

Console.ReadLine();
Console.ReadKey();

Full Source:

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
            Console.ReadLine();
        }
    }
}

C# Command Line Parameters

In some situations, you have to send command line parameters to your application. When you pass arguments, the command line input, read from the standard entry point as string array.

static int Main(string[] args)

The arguments of the Main method is a String array that represents the command-line arguments. Usually you determine whether arguments exist by testing the Length property.

 if (args.Length == 0)
{
  System.Console.WriteLine("No arguments found!!");
  return 1;
}

The main program accept arguments in the order of args[0], args[1] etc. The following program shows how to display all parameters enter to the console application.

using System;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Number of command line arguments = {0}",args.Length);
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
            }
        }
    }
}

How to pause console application

System.Threading.Thread.Sleep() will delay your programs. It receives a value indicating the number of milliseconds to wait. This can be useful for waiting on an external application or task.

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("Pausing 5 seconds");
    System.Threading.Thread.Sleep(5000);
  }
}

When you run the above program, it will wait for 5 seconds and exit from application.

How do I correctly exit from a console application

A console application will just terminate when it’s done, generally when it runs to the end of its Main entry point method. A “return” will achieve this.

using System;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("I am in the Loop !!!!");
                return; //This will exit the console application's running thread
            }
        }
    }
}

Console based applications will exit when the main function has finished running, though you must be careful to get rid of any lingering resources you might have been managing, as they can keep the underlying process alive. In some situations you may have to forcefully terminate your program. In this case you can explicitly exit using Environment.Exit.
SystemEnvironment.Exit();

If you’re returning an error code you can do it this way, which is accessible from functions outside of the initial thread:

System.Environment.Exit(-1);


C# windows based application

The following program is a sample Windows Based C# application. Create a new C# Windows Application project and copy and paste the following source code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			MessageBox.Show("This is my first program");
		}
	}
}

When you execute this C# program you will get the message “This is my first program” in message box.

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More