Labels

http://96b9164e.linkbucks.com

Sunday 24 February 2013

Lesson 1: Getting Started with C#

This lesson will get you started with C# by introducing a few very simple programs. Here are the objectives of this lesson:
  • Understand the basic structure of a C# program.
  • Obtain a basic familiarization of what a "Namespace" is.
  • Obtain a basic understanding of what a Class is.
  • Learn what a Main method does.
  • Learn how to obtain command-line input.
  • Learn about console input/output (I/O).

A Simple C# Program

There are basic elements that all C# executable programs have and that's what we'll concentrate on for this first lesson, starting off with a simple C# program. After reviewing the code in Listing 1-1, I'll explain the basic concepts that will follow for all C# programs we will write throughout this tutorial. Please see Listing 1-1 to view this first program.
Warning: C# is case-sensitive.
Listing 1-1. A Simple Welcome Program: Welcome.cs
// Namespace Declaration
using
System;

// Program start class
class WelcomeCSS
{
    // Main begins program execution.
    static void Main()
    {
        // Write to console
        Console.WriteLine("Welcome to the C# Station Tutorial!");
    }
}
The program in Listing 1-1 has 4 primary elements, a namespace declaration, a class, a Main method, and a program statement. It can be compiled with the following command line:
 csc.exe Welcome.cs
This produces a file named Welcome.exe, which can then be executed. Other programs can be compiled similarly by substituting their file name instead of Welcome.cs. For more help about command line options, type "csc -help" on the command line. The file name and the class name can be totally different.
Note for VS.NET Users: The screen will run and close quickly when launching this program from Visual Studio .NET. To prevent this, add the following code as the last line in the Main method:
// keep screen from going away
// when run from VS.NET

Console.ReadLine();

No comments:

Post a Comment