FREE BOOK

Chapter 1: Introduction to C#

Posted by Apress Free Book | C# Language December 08, 2008
This Chapter will introduce you to C#. You will learn how to write and compile C# programs, C# syntaxes, data types, control flow, classes and their members, inter-faces, arrays, and exception handling I'll begin with an overview of the language.

Writing Your First C# Program: "Hello, C# Word!" 
 
Writing a "Hello, World!" program is always the first step to learning any new programming language. In keeping with this tradition, your first C# program will write output on your console saying, "Hello, C# word!"
 
Before starting with the C# programming, however you must install the C# compiler. The C# command-line compiler, csc.exe, comes with Microsoft's .NET SDK.
 
The .NET SDK supports the Windows 98, Windows ME, Windows NT 4.0 and Windows 2000 platforms. After installing the compiler, insert the code for the "HELLO, C# World!" program, which is shown in Listing1-1. Then save the file as first.cs.
 
 Listing 1-1. "Hello, C# world!" code
 
 
using System;
 
class
Hello
 
{
     static void Main()
     {
         Console.WriteLine("Hello, C# world!");
     }
 }

 

You can compile C# code from the command line using this syntax:
 
 csc C:\ temp\ first. cs
 
Make sure the path of your .cs file is correct and that the csc executable is included in your path. Also make sure that path of C# Compiler (csc.exe) is correct. After compiling your code, the C# compiler creates an .exe file called first.exe under the current directory. Now you can execute the .exe from window explorer or from the command line. Figure 1-1 shows the output.
 
 
 
 Figure 1-1. "Hello, C# World!" program output
 
Did you see "Hello, C# world!" on your console? If you did, then congratulations, you're now officially a C# programmer. If not, then you may want to check the path of your file first.cs and the path of the compiler csc.exe. 
 
You have now written your first few lines of C# code. But what does each line of your program mean? I'll describe the various components of your "hello, C# world!" program. 
 
The first line of your program is this:
 
 
using System;
 
The .NET framework class library is referenced in namespaces. The System namespace contains the Console class, which reads from or writes to the console. 
 
The class keyword defines a new class that is followed by a class name, as seen in the second line of the "Hello, C# World!" code listing:
 
 
class Hello
 {
           ...
 }
 

 
The next line of code is the static void Main() function:
 
 
static void Main()
     {
         Console.WriteLine("Hello, C# world!");
     }

 
In C#, every application must have a static Main()or int Main() entry point. The concept is similar to that of the Main()function of C++. 
 
The Console class is defined in the System namespace. You can access its class members by referencing them directly. Writeline(), A method of the Console class, writes a string and a line terminator to the console.

Total Pages : 10 12345

comments