Creating the Source Program for Your First C# Console Application
Visual Studio 2008 includes an Application Wizard that builds template programs and saves you a lot of the dirty work you’d have to do if you did everything from scratch.
Typically, starter programs don’t actually do anything — at least, not anything useful. However, they do get you beyond that initial hurdle of getting started. Some starter programs are reasonably sophisticated. In fact, you’ll be amazed at how much capability the App Wizard can build on its own, especially for graphical programs.
The following instructions are for Visual Studio. If you use anything other than Visual Studio, you have to refer to the documentation that came with your environment. Alternatively, you can just type the source code directly into your C# environment.
To start Visual Studio, choose Start –> All Programs –> Microsoft Visual Studio 2008 –> Microsoft Visual Studio 2008.
Complete these steps to create your C# console app:
1. Choose File –> New –> Project to create a new project.
Visual Studio presents you with lots of icons representing the different types of applications you can create.
2. From this New Project window, click the Console Application icon.
Make sure that you select Visual C# — and under it, Windows — in the Project Types pane; otherwise, Visual Studio may create something awful like a Visual Basic or Visual C++ application. Then click the Console Application icon in the Templates pane.
Visual Studio requires you to create a project before you can start to enter your C# program. A project is like a bucket in which you throw all the files that go into making your program. When you tell your compiler to build (compile) the program, it sorts through the project to find the files it needs in order to re-create the executable program.
The default name for your first application is ConsoleApplication1, but change it this time to Program1.
The default place to store this file is somewhere deep in your Documents directory. You can change this default program location by following these steps:
• a. Choose Tools –> Options –> Projects and Solutions –> General.
• b. Select the new location (for example, C:C#Programs) in the Visual Studio Projects Location box and click OK.
Leave the other boxes in the project settings alone.
3. Click the OK button.
After a bit of disk whirring and chattering, Visual Studio generates a file called Program.cs. (If you look in the Solution Explorer window, you see some other files; ignore them for now. If Solution Explorer isn’t visible, choose View –> Solution Explorer.) C# source files carry the extension .CS. The name Program is the default name assigned for the program file.
The contents of your first console app appear as follows:
static void Main(string args)
Along the left edge of the code window, you see several small plus (+) and minus (-) signs in boxes. Click the + sign next to using…. This expands a code region, a handy Visual Studio feature that keeps down the clutter. Here are the directives when you expand the region in the default console app:
Regions help you focus on the code you’re working on by hiding code that you aren’t. Certain blocks of code — such as the namespace block, class block, methods, and other code items — get a +/- automatically without a #region directive. You can add your own collapsible regions, if you like, by typing #region above a code section and #endregion after it. It helps to supply a name for the region, such as Public methods. Here’s what this code section looks like:
#region Public methods
… your code
#endregion Public methods
This name can include spaces. Also, you can nest one region inside another, but regions can’t overlap.
For now, using System; is the only using directive you really need. You can delete the others; the compiler lets you know whether you’re missing one.