Day by day class
please see this blog for study c#
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.
1) Simple Welcome Program: Welcome.cs
// Namespace Declaration
using System;
// Program start classclass 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();
// when run from VS.NET
Console.ReadLine();
Note: The command-line is a window that allows you to run commands and programs by typing the text in manually. It is often refered to as the DOS prompt, which was the operating system people used years ago, before Windows. The .NET Framework SDK, which is free, uses mostly command line tools. Therefore, I wrote this tutorial so that anyone would be able to use it. Do a search through Windows Explorer for "csc.exe", which is the C# compiler. When you know its location, add that location to your Windows path. If you can't figure out how to add something to your path, get a friend to help you. With all the different versions of Windows available, I don't have the time in this tutorial, which is about C# language programming, to show you how to use your operating system. Then open the command window by going to the Windows Start menu, selecting Run, and typing cmd.exe.
The first thing you should be aware of is that C# is case-sensitive. The word "Main" is not the same as its lower case spelling, "main". They are different identifiers. If you are coming from a language that is not case sensitive, this will trip you up several times until you become accustomed to it.
The namespace declaration, using System;, indicates that you are referencing the System namespace. Namespaces contain groups of code that can be called upon by C# programs. With the using System;declaration, you are telling your program that it can reference the code in the System namespace without pre-pending the word System to every reference.
No comments:
Post a Comment