|
You have selected free tutorial of the Microsoft Corporation for the Microsoft Technology Associate (MTA) :
98-361: MTA: Software Development Fundamentals (C#) : Module 1: Introduction to Programming :
UNDERSTANDING THE STRUCTURE OF A C# PROGRAM
Microsoft Help:-
The C# command-line compiler is convenient for compiling and running C# programs
because you don’t have to create a project for the program. So it is a bit faster
and easier than using the IDE. Simply type the program in an editor, say Notepad.exe,
and save the file with the extension .cs in any of the folders on your disk drive. For
example, type the code as shown in below in Notepad and save it with the name
HelloWorld.cs.
using System;
class HelloWorldClass
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
To compile the program through the C# command-line compiler csc.exe, you need to
open a command prompt window that is configured for Visual Studio. Select the
Developer Command Prompt for VS2013 option from the Start menu. Thereafter,
using the CD command, go to the folder where you have saved the HelloWorld.cs file.
At the command prompt, execute the C# compiler csc.exe, specifying the name of the
source file, as shown here:
D:\CSharpBookCode>csc HelloWorld.cs
The csc compiler shows errors in your program if any exist. If there are no errors, the
csc compiler successfully compiles the program and creates a file called HelloWorld.exe
that contains the MSIL version of the program . Although MSIL is not
executable code, it is still contained in an .exe file.
The Common Language Runtime automatically invokes the JIT compiler after executing
the file HelloWorld.exe. To run the program, just type its name at the command
prompt, as shown here:
D:\CSharpBookCode>HelloWorld
When the program runs, the following output is displayed:
1 using System;
2 class HelloWorldClass
3 {
4 static void Main(string[] args)
5 {
6 Console.WriteLine("Hello World!");
7 }
8 }
- Line 1 indicates that the program is using the System namespace. A namespace defines
a logical grouping that organizes related classes, structures, delegates, enumerations,
interfaces, and other types. The idea of enclosing these members in namespaces is to
avoid name conflicts. A class named A defined in namespace1 does not conflict with the
class named A defined in namespace2. By using or importing a namespace in a program,
you enable the importing program to access the members organized in the namespace.
Because you are using the library class System.Console in your program, the using
System; statement allows you to refer to this class simply as Console (without qualifying
it by namespace as System). That is, you can access any of the members defined in the
System namespace directly without qualifying it with the System namespace if you
import it at the top of the program with the using statement. Most of the .NET types
are defined in the standard System namespace. In this program, the goal is to access the
WriteLine() method of the Console class defined in the System namespace for displaying
a text message on the screen. The Console is the .NET base classes. Besides the
Console class, there are many other classes defined in the System namespace that are
used in almost every C# program. Therefore, you will be importing the System namespace with the using statement in almost all C# programs.
- Line 2 uses the keyword class to declare that a new class is being defined. The class is
used for encapsulation. The name of the class is HelloWorldClass. The class definition
begins with the opening curly brace ({) and ends with the closing curly brace (}). The
elements between the two braces are members of the class.
Line 4 defines the Main() method and is the entry point of a C# program. (That is, it
begins its execution from the Main() method.) The method has the static modifier,
which means it can be called before an object of this class is created. This is because
Main() is called at program startup. The keyword void indicates that the Main() method
does not return a value. The Main() method has a single parameter: an array of strings
(string[] args). You can use this parameter for passing command-line arguments. The empty parentheses that follow Main indicate that no information is passed to Main(). The opening curly braces ({) signal the start of Main( )’s body. All the code that comprises a method occurs between the method’s opening and its closing curly brace. For displaying output on the screen, you use the method WriteLine() of the Console class, as shown in line 6. The string passed to the WriteLine() method is displayed on the screen. Information that is passed to a method is called an argument. The line begins with Console, which is the name of a predefined class that supports console input/output (I/O). By calling the WriteLine( ) method of the Console class, you display the desired output on the screen.
|
Your Salary Above $ 66000... Click ...
Ohh! You want More.... be game developer of your choice $ 102000 ....
|