Csharp – Console Application

A Simple C# Console Application

What we’re going to do first is to create a very simple program, so that you can see what makes up a C#.NET project. By the end of this chapter, you’ll have learned the following:

  • How to create new projects
  • What the Solution Explorer is
  • The various files that make up of a C#.NET project
  • How to save your work
  • How to run program’s
  • The importance of the Main statement

The simple program we’ll create is called a Console Application. We won’t be doing much else with this type of application

We are using Visual Studio 2010 0r 2012 for this course. You can also use Visual C# Express for this course

A Console Application is one that looks like a DOS window. If you don’t know what these are, click your Start menu in the bottom left of your screen. Click on Run from the dialogue box that appears, type cmd:

In Vista and Windows 7, type cmd in the search box at the bottom of the start menu. In Windows 8, the search box is on the Start Screen page. You’ll then see the search results appear:

csharp-tutorials
csharp-tutorials

Click cmd.exe to see the console appear.

Click OK and you’ll see a black screen, like the below screenshot:

csharp-tutorials
csharp-tutorials

This is the type of window you’ll see for our Console Application.So with Visual C# Express open, click File from the menu bar at the top. From the File menu, select New Project (or click the New Project link on the left of the opening screen in versions 2010 and 2012):

csharp-tutorials
csharp-tutorials

When you click on New Project, you’ll see the following dialogue box appear:

csharp-tutorials
csharp-tutorials

Or this one in version 2010 of the software:

csharp-tutorials
csharp-tutorials

For 2012 users, click on Templates from the list on the left. Under Templates, click on Visual C#. You’ll then see Console Application appear in the middle:

csharp-tutorials
csharp-tutorials

For all versions, the New Project dialogue box is where you select the type of project you want to create. If you only have the Express edition of Visual C#, the options are limited. For the rest of this book, we’ll be creating Windows Applications. For now, select Console Application. Then click OK.

 

When you click OK, a new Console Application project will be created for you. Some code should be displayed:

csharp-tutorials
csharp-tutorials

As well as the code, have a look on the right-hand side and you’ll see the Solution Explorer. This is where all the files for your project are. If you can’t see the Solution Explorer, click View from the C# menu bar at the top. From the View menu, click Solution Explorer.

The code itself will look very complicated if you’re new to programming. For now, right-click the Program.cs tab at the top, and click Close from the menu that appears:

csharp-tutorials
csharp-tutorials

Now double-click the Program.cs file in the Solution Explorer:

csharp-tutorials
csharp-tutorials

When you double-click Program.cs, you should see the code reappear. So this code is the program that will run when anyone starts your application

Now click the plus symbol next to Properties in the Solution Explorer above. You’ll see the following:

csharp-tutorials
csharp-tutorials

The file called AssemblyInfo.cs contains information about your program. Double-click this file to open it up and see the code. Here’s just some of it:

csharp-assembly-info
csharp-assembly-info

The reddish color text is something you can change. You can add a Title, Description, Copyright, Trademark, etc.

But right click the AssemblyInfo.cs tab at the top, and click Close from the menu. Now, in the Solution Explorer, click the plus symbol next to References:

csharp-assembly-info2
csharp-assembly-info2

These are references to code built into C# (you won’t see as many entries in earlier versions of the software). Much later, you’ll see how to add your own files to this section

Before we add some code, let’s save the project. We’ll do that in the next part below.

When you save your work, C# will create quite a few folders and files for you. Click File from the menu bar at the top of the Visual C# Express software, then Save All:

csharp-tutorials
csharp-tutorials

When you click Save All, you’ll see the following dialogue box appear Visual Studio Express 2008 and 2010: (2012 users won’t need to do anything here, as this above information was displayed when you created a new project earlier.)

csharp-tutorials
csharp-tutorials

You can type any name you like for your project. The default Name is ConsoleApplication1. Have a look at the location of the project, though:

 

C:\Users\Owner\documents\visual studio 2010\Projects

In XP, however, you’ll see something like this

C:\Documents and Settings\kayspc\My Documents\Visual Studio 2008\Projects

So it’s going to be saved to the “documents” folder of this computer. In the “documents” folder you’ll find another one called Visual Studio 2010. In this folder, there will be one called Projects.

Before clicking the Save button, make sure there is a tick in the box for “Create a directory for solution”. Then click Save.

Now open up Windows Explorer (Hold down the Windows key on your keyboard, then press the letter “e”). Navigate to the folder location above. In the image below, we’ve used Windows Explorer to navigate to the Visual Studio 2010 folder:

csharp-tutorials
csharp-tutorials

Double click the Projects folder to see inside of it. You should see a folder calledConsoleApplication1. Double click this folder and you’ll see the following:

csharp-tutorials
csharp-tutorials

So there’s another folder called ConsoleApplication1. There’s also two files: one that ends in sln, and one that ends in suo. The sln file is the entire solution. Have a look at the Solution Explorer again:

csharp-tutorials
csharp-tutorials

The one highlighted in blue at the top refers to the sln file. The suo file contains information about the Visual Studio environment – whether the plus symbols are expanded in the Solution Explorer, what other files you have open in this project, and a whole host of other settings. (If you can’t see the suo file click Tools > Folder Option in Windows Explorer. In Vista and Windows 7, you may have to click Organise > Layout > Menu Barfirst. Click the View tab, and select the option for “Show hidden files and folders”.)

Double click your ConsoleApplication1 folder, though, to see inside of it:

csharp-tutorials
csharp-tutorials

Now we have three more folders and two files. You can see the bin and obj folders in the Solution Explorer:

csharp-tutorials
csharp-tutorials

Click ConsoleApplication1, second from the top. Then click the icon for Show all Files, circled in red in the image above. To see All Files in version 2012, click the symbol circled in the image below

csharp-tutorials
csharp-tutorials

The bin and obj folders will appear. Click the plus symbol or arrows to see what’s inside of these folders:

csharp-tutorials
csharp-tutorials

The important one for us is the Debug folder under bin (there’ll be an extra file ending in .manifest in c# 2010). You’ll see why it’s important in a moment. However, it’s time to write some code!