How to Create a C# Console Application
C#

How to Create a C# Console Application

In this post, I will explain how to create a C# console application. As a matter of fact, Visual Studio makes it a lot easier to develop and run the applications. In general, the .NET framework supports many programming languages and C# is one among them. Although, we can use C# to create many different types of applications, here we focus on creating console applications.

C# Console Applications

A C# console application runs on the command prompt and takes input from the console and also displays output on the console. In order to create a C# console application, follow the steps given below:

Step 1. Start Visual Studio 2019 and click on Create a new project.

Create a Project in Visual Studio 2019
Create a Project in Visual Studio 2019

Step 2. Select the right template with language as C# as ashown below and click on Next.

Select C# Console App
Select C# Console App

Step 3. Specify the name of application and click on Next.

Specify Project Name
Specify Project Name

Step 4. Write application code in the code editor.

Code Editor of Visual Studio
Code Editor of Visual Studio

Step 5. Save and run the applications.

Output on Console
Output on Console

Important Points

Before proceeding further to write some more applications, there are a few important points to remember.

  1. A C# application makes use of the .NET library which is available in the form of namespaces. To emphasize further, a namespace is a logical collection of classes used for the purpose of removing name collisions.
  2. System namespace contains fundamental classes and Console is one of the classes available in System namespace.
  3. using keyword imports the classes defined in a namespace.
  4. A C# program is organized in namespaces and the name of the project that we choose when creating the application becomes the name of the namespace. Hence, in the above program Application1 is the name of the namespace which contains class definitions.
  5. C# is an object-oriented language. Hence, we need to write code only within the scope of a class in C#. Program is the default name of the class. However, you can change it.
  6. The execution of the program starts with the Main() method. The main() method is static. Hence, it is executed without the help of any object.
  7. WriteLine() method is available in the Console class and displays the value on the console.

Now, write another simple C# console application for adding two numbers that the user provides as input. For this purpose, create another project and write the following code.

using System;
namespace Application2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            Console.WriteLine("Enter First Number ");
            a = Int32.Parse(Console.ReadLine());

            Console.WriteLine("Enter Second Number ");
            b = Int32.Parse(Console.ReadLine());

            c = a + b;
            Console.WriteLine("Sum = " + c);
        }
    }
}

Finally, run the program and enter the numbers to add. The program will show the sum on the console.

Sum of two numbers
Sum of two numbers

In the above program, ReadLine() function available in the console class is used to take input from the keyboard. Since the ReadLine() function only returns strings, we need to convert it into integer before assigning it into a variable. Basically, we use the Parse() function of the class Int32 for converting a string into an integer value.

To summarize, a console application in C# workes with the command prompt. The WriteLine() and ReadLine() functions of the Console class are used to show output and read input values from the console respectively. Finally, as the ReadLine() function returns only strings, we need to convert the values into appropriate data types before using.


Further Reading

C# Examples

Leave a Reply

Your email address will not be published. Required fields are marked *