1. Open Visual Studio

2. In the toolbar, go to File β†’ New Project

3. Select the Console Application project type

4. Open the file Program.cs in the Solution Explorer

5. Add the following code to Main():

 public class Program 
{  public static void Main()  {  // Prints a message to the console.
  System.Console.WriteLine("Hello, World!"); 
 /* Wait for the user to press a key. 
This is a common  way to prevent the console window from terminating 
 and disappearing before the programmer can see the contents  
of the window, when the application is run via Start from within VS.
 */  System.Console.ReadKey();  } } 

6. In the toolbar, click Debug -> Start Debugging or hit F5 or ctrl + F5 (running without debugger) to run the program.

class Program is a class declaration. The class Program contains the info and method definitions that your program uses.

Classes generally contain multiple methods. Methods define the behavior of the class.
However, the Program class has just one method: Main. static void Main() defines the most method, which is that the entry point for all C# programs.

The Main method states what the class does when executed. Only one Main method is allowed per class.

System.Console.WriteLine(“Hello, world!”); method prints a given data (in this instance , Hello, world!) as an output in the console window.System.Console.ReadKey(), ensures that the program won’t close immediately after displaying the message.

It does this by waiting for the user to press a key on the keyboard. Any key press from the user will terminate the program.

The program terminates when it has finished the last line of code in the main() method.

Using the command line

To compile via command line use either MSBuild or csc.exe (the C# compiler), both part of the Microsoft Build Tools package.To compile this example, run the following command in the same directory where HelloWorld.cs is located:

 %WINDIR%Microsoft.NETFramework64v4.0.30319csc.exe HelloWorld.cs 

It also can be possible that you simply have two main methods inside one application. In this case, you have to tell the compiler which main method to execute by typing the subsequent command within the console.

(suppose Class ClassA also features a main method within the same HelloWorld.cs enter HelloWorld namespace).

 %WINDIR%Microsoft.NETFramework64v4.0.30319csc.exe HelloWorld.cs /main:HelloWorld.ClassA 

where HelloWorld is namespace

 dir %WINDIR%Microsoft.NETFrameworkcsc.exe /s/b dir %WINDIR%Microsoft.NETFramework64csc.exe /s/b 

There should now be an executable file named HelloWorld.exe in the same directory. To execute the program from the command prompt, simply type the executable’s name and hit Enter as follows:

 HelloWorld.exe 

This will produce:

Hello, world!

You may also double click the executable and launch a new console window with the message “Hello, world!”

If you like this post, don’t forget to share πŸ™‚

This article is written by our awesome writer
Comments to: Creating a new console application in C#

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password