developmentor - a developer services company				  
				      C# Tutorial
   Modules

.NET Overview


Goals:

  • Write and run a simple C# program from the command line.
  • Write and run a simple C# program using Visual Studio.NET.

Overview

Here we will write a simple C# program two ways, first using simple tools such as notepad and a command window and second using the sophisticated development environment Visual Studio 2010.


part 1- Command Line

Create and run a C# program using command line tools.

steps:

  1. Create a directory to contain your work.

  2. Open a command window in your work directory. The Visual Studio 2010 installation includes the Visual Studio 2010 Command Prompt which will already have all the required paths set correctly. To use a regular command window, you may need to add the directory containing the compiler to you PATH environment variable. The compiler should be located in the directory: "C:\WINDOWS\Microsoft.NET\Framework\vx.x.xxxx" where the last component is the version number of the installed .NET Framework. Environment variables can be accessed from the "Advanced" tab in "Computer Management Properties". Another option is to run the script vcvars32.bat to set the appropriate environment variables. The script is usually located in: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin.

  3. Use notepad to write a simple C# program with a namespace, a class, and a Main method. Print a message to the console from the main method. Save the program in a file with a .cs extension.

  4. Compile the program by running the C# compiler csc from the command window.

  5. Run the program from the command window.

  6. Use the compiler /out:<name> option to choose a different output file name. Run the program again using the new name.


part 2- Visual Studio 2010

Create and run a C# program using Visual Studio 2010.

Visual Studio 2010 gives you two constructs to organize your source code: solution and project.

A solution is the highest level of organization and can contain one or more projects. The intuition for a solution might be that it represent the entire application under development. An application might contain multiple source files that get compiled into an exe and several supporting dll's. A single solution can encompass all these.

A project is a lower level organizing structure and typically corresponds to a target such as a dll or exe. A project can contain one or more source files. When you create a project, Visual Studio 2010 automatically creates a source file and provides suggested starter code based on the type of the project. Feel free to take advantage of any of this code that is appropriate.

For theoretical purity, the cleanest organization for the lab exercises is probably to create a new solution for each activity. This is because each activity may have a few unrelated parts. Within an activity, you can then make a new project every time the topic changes. Often this will correspond to a new project for each 'part' of an activity, but not always since many times several parts build on one another and so belong in the same project.

A more pragmatic option is to make a single solution for the whole course. This option can reduce the overhead required - you will not need to spend as much time making new solutions. Also, it will be much easier to go back to examine previous work since it is all in the same solution. The instructions in the exercises follow this approach.

When you have more than one project in a solution, you can switch among the projects by making the project of interest the "startup project". Simply right click on the project and choose Set as Startup Project. The startup project is executed when you select Debug-->Start. The startup project will be displayed in boldface in the solution explorer.

steps:

  1. Start Visual Studio 2010.

  2. Use File-->New to create a new blank solution.

    Navigate to save the solution in a directory of your choice.

    Name the solution "IntroCSharp".

  3. Use File-->New to create a new project.

    Select "Console Application" from the list of Visual C# Project types.

    Select the "Add to Solution" radio button.

    Name the solution "DotNetOverview".

  4. Add a line to Main that prints a message to the console.

  5. Build the solution using the Build menu or the icon on the Build toolbar.

  6. Run the program using Start on the Debug menu or the icon on the Debug toolbar.

    Notice that the console window disappears after the program finishes execution. A call to Console.Read() can be inserted at the end of your code to cause the program to stop and wait for input. This will allow you to easily view the output of your program. Type the Enter key to satisfy the blocked call to Read and end the program.

Solutions:

This material is excerpted from the Programming C# course offered by DevelopMentor.