{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
First Console Program in C# Explained
Summary
This article explains writing the first console application using .NET Framework and C#. The console applications do not have any graphical user interfaces. These are also called command line applications. These are the applications that run in the good old DOS window (or Command Window, as it is called nowadays).
 
Table of Contents

New Console Project

Figure 1. New Project window for a Console Application

The Solution and the Project

Figure 2. Solution Explorer window for the First Console application

The Generated Source

Listing 1. Main generated source for the Console Application project

The Namespaces from .Net

Namespace Enclosure

The Class

The Main Method

Doing the Hello World

Listing 2. C# Hello World for a Console program

 
Article Series
Previous Article: This is the First Article of the Series
This article is part of the Series:
Article Series: First Programs in C# Explained
Next Article:
Test Your Knowledge: First Console Program in C# Explained

In this article, let’s write a quick Hello World program for the console using C#. This is one of the simplest kind of programs you can develop using C#. The console applications do not have any graphical user interfaces. These are also called command line applications. These are the applications that run in the good old DOS window (or Command Window, as it is called nowadays).

Small server applications are typically written as console applications. This type is also ideal for things that are to be done just once (or a few times). For example, you are cleaning up some data (and it needs to be done just once), then you will develop a console application and run it once or twice and forget about it.

In the old days, there used to be a lot of console applications, because developing a graphical front end often involved a lot of programming. And since computers weren’t as powerful as they are nowadays, you would shy away from anything that took more computing power than necessary.

As a professional programmer, you would still develop plenty of console applications during your career. For certain applications (compilers, for example), a console application provides enormous flexibility. For example, a compiler might take any of a hundred flags/parameters. A GUI application will categorize them into, say, ten screens; however, a power user will be able to specify the small number of parameters required for a certain task much faster from the command line than from the GUI.

New Console Project

This project uses Visual Studio 2008. Choose File – New Project from the menu to open the New Project window as shown in Figure 1.

Figure 1. New Project window for a Console Application

New Project window for a Console Application

Visual Studio provides templates that set up all the required basic files for project you are about to develop. In this case, we will be using the ‘Console Application’ template, which is under Visual C# - Windows Project Type in the New Project window.

A few notes on this New Project window for developing Console Applications:

At the top-right of the window, you will see .NET Framework 3.5 in the dropdown box. If you click this dropdown, you will see two other options: .NET Framework 2.0 and .NET Framework 3.0. If you are developing for older versions of .Net (for example, you want to install this application on the computers that only has .Net Framework 2.0), then you can choose that particular version. Only the capabilities (classes, etc.) that are available in that version are available to your program.

If you have Version Control System (like SourceSafe), you can check the box ‘Add to Source Control’ to automatically let Visual Studio add this project to the source control system. Visual Studio will also make it easy to check in and check out the source files as you change them.

The Solution and the Project

After providing the necessary information and clicking OK in the New Project window, Visual Studio will create new solution and project for you to start developing a console application.

Figure 2. Solution Explorer window for the First Console application

New Project window for a Console Application

As shown in Figure 2, Visual Studio creates a solution named FirstConsole for you. If you look in the directory where you saved this (in the New Project dialog box), you will see a file called FirstConsole.sln. This is a text file that contains information about the projects that this solution holds.

Then you will also see a project called FirstConsole underneath the solution. Under the subdirectory FirstConsole, you will find a file called FirstConsole.csproj (the C# project file for this project). This is also a text file that contains the information about the source files, references, build related information, etc.

The Generated Source

Now let’s look at the main source file that’s generated for us by Visual Studio. This file is Program.cs.

Listing 1. Main generated source for the Console Application project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FirstConsole
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Listing 1 shows the C# source generated in Program.cs. Let’s go over various pieces of this code from top to bottom.

The Namespaces from .Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

A C# program can readily use the functionality available in the .Net Framework library. The .Net Framework library itself is organized into various Namespaces. Each namespace contains a set of related classes. And, of course, each class contains methods, properties, events, etc. to support a specific set of functionality.

In order to use the classes from a given namespace easily, you will use the using keyword. So, by specifying “using System;”, you will be able to use all the classes in that namespace (without fully specifying the path that particular class).

You will see the System namespace in every .Net application, because it contains all the basic classes. The most important class from the perspective of a console application is the Console class, which is in the System namespace.

By default, Visual Studio also includes other generally used namespaces like System.Collections.Generic, System.Linq, and System.Text. In the Hello World application below, we won’t use classes from these namespaces, but in a decent sized applications, you are likely to use the classes from these namespaces.

Namespace Enclosure

namespace FirstConsole

This line says, all the code that’s enclosed below (in the curly braces {}) belongs to the namespace FirstConsole. This means, if somebody includes using FistConsole; in their program, the public classes from here will be available for them to use.

The Class

    class Program

This line says, we are creating a class called Program. A class is an object-oriented construct that contains methods, properties, events, etc.

The Main Method

        static void Main(string[] args)
        {
        }

By default, this class has an empty method called Main. The code you write in the Main method is the first piece of code that gets executed when you run this program. Meaning, Main is the starting point for this program.

There are two keywords in front of the Main method – static and void. The keyword static says, this method can be executed without creating an instance of this class. And the keyword void says that this method does not return any value.

On to the right of Main, you see string[] args, which means, this method received a parameter called args. This parameter args is an array of strings. Here, args are the arguments you pass to the program when you execute it from the command line.

In all: Main is a method that can be called without creating an instance of class, and it does not return any values. And it takes an array of strings as a parameter.

Doing the Hello World

The above program compiles and runs if you press F5. However, it doesn’t do anything. Let’s add a line that prints Hello, World in the command window.

Listing 2. C# Hello World for a Console program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FirstConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

We just added the following line to the Main method.

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

The Console class (which is in the System namespace) has several static methods (methods that can be executed right on the class name, instead of having to create an instance of the class). One of these static methods is WriteLine(), which writes the supplied string to the console.

If you press F5 now, you will see the Hello, World! written to the console. If the console closes pretty fast, place a breakpoint below the Console.WriteLine line to see the console window longer.

Take a Quick Quiz on this Article

1. Which of the following programs is NOT an ideal console project?



: Pretty programs typically want GUI
Question 1 of 10
Article Series
Previous Article: This is the First Article of the Series
This article is part of the Series:
Article Series: First Programs in C# Explained
Next Article:
Test Your Knowledge: First Console Program in C# Explained
Bookmark and Share This

More Articles With Similar Tags
icon-first-web-new-project.jpg
This article explains and dissects the pieces involved in creating the first web program in C# using the Web Project method (as opposed to the Web Site method).
How do you develop an ASP.NET program using the Web Project model?
icon-first-web-site-solution-explorer.jpg
This article explains and dissects the pieces involved in creating the first web program in C# using the Web Site method (as opposed to the Web Program method).
What is involved in developing a web-based program using the C# programming language with Web Site - Solution as the model.
This test has questions on developing first console (shell) based programs using the C# programming language.
About  Contact  Privacy Policy  Site Map