Today, I am going to talk about how to create a C# console application using Visual Studio 2010. To do this, you will need the following:
- A PC running Windows
- An installed copy of Microsoft Visual Studio 2010. (Click here to get the free edition directly from Microsoft.)
Creating a Visual Studio project
To begin, open Visual Studio and select from the menu bar File->New->Project. In the New Project dialog box, you will see a tree view of the Installed Templates that are available. Choose Visual C# -> Windows, then select Console Application in the center. In the Name textbox below, enter the name, “CSharpConsoleAppDemo1″. I recommend that you create a new folder under the Visual Studio 2010 folder called “Demos” and change the Location textbox to create the project there. Separating your real projects from “learning” projects can be useful in the long run. The solution option to “Create a new solution” should be left alone. Finally, the solution name should already be the same as your project name, so that also should be left alone.
You can read more about creating projects and solutions here.
Once this is completed, you should see your solution, “CSharpConsoleAppDemo1″, in the Solution Explorer which is a tree view of your solution and all of its projects. Under this is your project, “CSharpConsoleAppDemo1″. Under that are three items, Properties, References, and Program.cs. The contents of the Program.cs file are what we are concerned with for the moment and that should be open in a tabbed document in the middle of the screen.
You can read more about Solution Explorer here .
In Program.cs, you should already have four standard “using” directives, a namespace declaration, and a class definition for Program. Using directives and namespace declarations work together to allow the compiler to recognize and use types defined in other files.
In the Program class, you will find the Main method. This is the entry point for your console application.
As a fast introduction, all that we shall have our console application do is display a simple message, “Hello, world!”, a familiar exercise for anyone has ever learned to program. To accomplish this, simply add the following line inside the curly braces of the Main method:
Console.WriteLine("Hello, world!"); Console.Read();
Console is a class in the .NET framework that represents the standard input, output, and error streams used for console applications. It contains various methods, such as WriteLine and Read, that can be used to display messages and get user input.
You can read more about the Console class here.
Building your project in Debug mode
Press F5 to build and debug your application. At this point, a console window will open with the message “Hello, world!”. Press any key and the window will automatically close. Since everything went according to plan, you are done. The application is a success.
In order to get the executable file, you will need to go into the folder where your project is located. Right click on the project name in the Solution Explorer and click on “Open Folder in Windows Explorer”. You should see folders named “bin”, “obj”, and “Properties”. You should also see two files named “CSharpConsoleAppDemo1.csproj” and “Program.cs” respectively. Double click on the bin folder. Inside this folder are the binaries created during the build process.
You should see a “Debug” folder. If you checked other bin folders, you may see other folders, particularly one named “Release”. These names correspond to the build configuration. Since the default build configuration for debugging is “Debug”, this is why you see a Debug folder. Unless you build using a Release configuration, you won’t see a Release folder at this point. Open the Debug folder by double clicking on it.
You should now see four files. The only one that you are concerned with at this time is the file named ”CSharpConsoleAppDemo1.exe”. This is the executable file that you just build. Although you would not normally distribute this file, you could easily copy this file and give it to all your friends and they could have the same “Hello, world!” application that you have. To verify that it does work, double-click on this file and see what happens. You should have seen the same window that popped up when you hit F5 in Visual Studio.
Building your project in Release mode
Go back to Visual Studio and you will see the word “Debug” as an item in a pulldown menu next to a green arrow. Click on this menu and you will see a few choices that include “Debug”, “Release”, and “Configuration Manager”. Select “Release” and hit F5. As before, a window should appear with the familiar “Hello, world!” message. Again, right-click on the project in Solution Explorer and select “Open Folder in Windows Explorer”. Double click on the folder and you should now see two folders, “Debug” and “Release”. Double click on “Release”. You should again see four files including one named ”CSharpConsoleAppDemo1.exe”. Double click on this file and again, the window containing the “Hello, world!” message appears. This is the release build of the console application and the one that is normally distributed to other users. The reason is that the Release version has code optimizations that complicate debugging.
You can read more about the differences between Debug and Release here.
Today, I discussed how to create a simple C# console application using Visual Studio 2010. This was a fairly trivial example, but it should provide a solid starting point for creating your own console applications.