for RuBoard |
As our first exercise in using Visual Studio, we will create a simple console application. Our program Bytes will attempt to calculate how many bytes there are in a kilobyte, a megabyte, a gigabyte, and a terabyte. If you want to follow along on your PC as you read, you can use the Demos directory for this chapter. The first version is in Bytes\Step1 . A final version can be found in Bytes\Step3 .
From Visual Studio main menu choose File New Project.... This will bring up the New Project dialog.
For Project Types choose "Visual C# Projects" and for Templates choose "Empty Project."
Click the Browse button, navigate to Demos, and click Open .
In the Name field, type Bytes . See Figure A-6. Click OK.
At this point you will have an empty C# project. We are now going to add a file Bytes.cs, which contains the text of our program.
In Solution Explorer right-click over Bytes and choose Add Add New Item.... This will bring up the Add New Item dialog.
For Categories choose "Local Project Items" and for Templates choose "Code File."
For Name type Bytes.cs . See Figure A-7. Click Open.
In the Solution Explorer double-click on Bytes.cs . This will open up the empty file Bytes.cs in the Visual Studio text editor. Type in the following program, and notice things like color syntax highlighting to indicate reserved words as you type.
// Bytes.cs using System; public class Bytes { public static int Main(string[] args) { int bytes = 1024; Console.WriteLine("kilo = {0}", bytes); bytes = bytes * 1024; Console.WriteLine("mega = {0}", bytes); bytes = bytes * 1024; Console.WriteLine("giga = {0}", bytes); bytes = bytes * 1024; Console.WriteLine("tera = {0}", bytes); return 0; } }
Besides the color syntax highlighting, other features include automatic indenting. All in all, you should find the Visual Studio editor friendly and easy to use.
You can build the project by using one of the following:
Menu Build Build
Toolbar
Keyboard shortcut Ctrl + Shift + B
You can run the program by using one of the following:
Menu Debug Start Without Debugging
Toolbar
Keyboard shortcut Ctrl + F5
You will see the following output in a console window that opens up:
kilo = 1024 mega = 1048576 giga = 1073741824 tera = 0 Press any key to continue
We will investigate the reason for the strange output later. If you press any key, as indicated, the console window will close.
You can run the program in the debugger by using one of the following:
Menu Debug Start
Toolbar
Keyboard shortcut F5
A console window will briefly open up and then immediately close. If you want the window to stay open, you must explicitly program for it, for example, by asking for input. You can set a breakpoint to stop execution before the program exits. We will outline features of the debugger later in the chapter.
for RuBoard |