Net-informations.com
SiteMap  | About    

C# console based application

How to start C# programming

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.

What is Console Application in C#

Here in the following program, we just 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.

Why is the console window closing immediately without displaying my output

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 you program is finished. When console applications have completed executing and return from their main method, the associated console window automatically closes. This is expected behavior.

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 key press 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);





net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.