"C # Introduction and improvement" (ii)

Source: Internet
Author: User
Tags array command line microsoft website visual studio

Chapter One development environment of C #
C # Most simple editor can use Notepad, but I do not recommend using it to edit the source code. The reason is that if you're dealing with a real programming language, compiling with Notepad can produce a lot of error message lines, but you don't know where it is.
You have several options:
First, you configure Visual C + + 6.0 in Visual Studio 6 to enable it to work with C # source files.
Second, choose Microsoft's latest visual Studio 7, its IDE style is very cool oh, incredibly still have vfp,c++, VB, C # in the same environment, but very resources, so use it to think clearly oh.

Let me introduce you to the installation of Visual Studio 7:


The choice of operating system, it is best to choose WIN2000 Chinese version (English version also can, Chinese of course in Chinese version), after installation upgrade to SP1 (ServicePack 1, Chinese version of the Chinese version of the WIN2KSP1), then please install IE5.5. Install Q274294_W2K_SP2_X86_CN. EXE patch, so far, the basic requirements have been reached.

Start installing Vs.net, unzip the downloaded files or copy the 1 and 2nd discs you purchased to a new directory on a hard drive (approximately 850M space), and then run the setup file after entering the directory. If it is installed in the Chinese version of the WIN2000 will need to enter the Setup directory, and then run the inside of the setup program.

The installer prompts you to install some incompatible versions of the components, mainly including FrontPage Server Extension qfe,ie5.5, and please place the directory where you specified the upgrade components or the 3rd CD. After the upgrade, the system installs related components such as XML3.

After the components are upgraded, the system will begin to install vs.net. Time is approximately 35 minutes (time is configured according to your machine)
In the above installation process, restart your computer if necessary, and most of the above mentioned can be downloaded from the Microsoft website (http://www.microsoft.com).
In fact, as long as you install the Ngws.net SDK, you can directly compile the C # program. When the NGWS SDK is installed, the installer automatically adds the path of the C # compiler to your environment variable path. So you can write a C # program with any editor, with the suffix name saved as. CS, and then execute the CSC xxx.cs at the command line to compile the build exe file.

Third, you can use any Third-party program editor, preferably to support line number, color coding, tool integration and good search capabilities, I recommend one (Sharp develop, a dedicated development tool for C #).

The IDE interface is as follows:

Now take the example "Hello World" to say how to use C # 's development environment.
The simplest "Hello World" program, as follows:

1:class HelloWorld
2: {
3:public static void Main ()
4: {
5:system.console.writeline ("Hello World");
6:}
7:}

In C #, code blocks (statement groups) are enclosed by large brackets ({and}). So, even if you have no experience in C + +, you can say that the main () method is part of the HelloWorld class statement because the class is enclosed in the defined braces.
The entry point of the C # application is the static Main method, which is a bit like C + +, but different in capitalization, it must be contained in a class. Only one class can use the flag definition, unless you tell the compiler which Main method it should use, no side, and a compilation error.
In contrast to C + +, Main's first letter is uppercase M, not the lowercase letter you used. In this method, your program starts and ends. Method can call other methods--in this case, to output text--or to create an object and activate the method.
As you can see, the main method returns a void type.
public static void Main ()
While these statements are seen, the C + + programmer will certainly feel familiar, but not other programmers. First, the public access flag tells us that this method can be accessed by any program, which is necessary for it to be invoked. Second, static means that you can call a method without creating an instance of the class first-all you have to do is call the method with the class name.
Helloworld.main ();
Another important aspect is the return type. For method Main, you can choose void (meaning there is no return value at all), or an int is an integer result (the error level returned by the application). Therefore, the two possible main methods are:
public static void Main ()
public static int Main ()
C + + Programmers will also know what I'm going to mention later--an array of command-line arguments that can be passed to the application. Such as:
public static void Main (string[] args)
I don't want to elaborate on how to access the parameters at this time, but I'd like to give a warning to C + + programmers beforehand: The application path is not part of this array compared to C + +. Only those parameters are included in this array.
After a brief introduction to the main method, let's focus on the only real line of code-the line of code that displays "Hello wold" on the screen.
System.Console.WriteLine ("Hello World");
If it weren't for system, people would immediately guess that WriteLine is a static method of the console object. So what does system represent? It is the namespace (scope) that contains the console object, not actually the prefix of the namespace in front of the console object each time, and you can introduce namespaces in your application as shown in Listing 3.2.
To introduce a namespace in your application, the code is as follows:


1:using System;
2:class HelloWorld
3: {
4:public static void Main ()
5: {
6:console.writeline ("Hello World");
7:}
8:}


All you have to do is give the System namespace a using (a bit like the uses in Delphi) instruction. After that, you no longer need to specify namespaces, you can use their methods and properties.

Compiling the application
Since NGWS runtime supports all compilers (VB, C + +, and C #), you do not have to buy a separate development tool to compile the application into IL (intermediate language). However, if you've never compiled an application from a command-line compiler (just compile the name and not memorize it), it's still your first choice.
Open a command prompt and switch to the directory where the HelloWorld.cs is stored. Typing the following command: CSC helloworld.cs
HelloWorld.cs is compiled and linked to Hellworld.exe (C # compiler if no error prompted)

To compile an application using the command line compiler csc.exe
Now you are ready to run the first application that is actually written in C #. Simply typing HelloWorld on the command line, the output is "Hello world".
Before proceeding, I would like to imagine the first application and the use of a compiler switch: Csc/out:hello.exe HelloWorld.cs
This switch tells the compiler that the output file is named Hello.exe. Although this is not a trick, it is the basic skill of the future compilers used in this book. Summary
In this chapter, you complete the first C # application from 0 to the creation, compilation, and execution. Learning is like this, from scratch, otherwise, they know, but also to learn, what is the significance of it!




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.