Beginner with C #

Source: Internet
Author: User
Tags define definition readline
1 Introduction
C # is a concise, stylish (? ), object-oriented (Object oriented), type-reliable (type-safe)
Programming languages. It (pronounced: C sharp) is a development from C + + + +. I think more like Java), and C/s + +
is a family language. Therefore, it is easy to be accepted by C + + programmers. The goal of C # is to combine the productivity of Visual Basic with
The simple power of C + +.

C # will be a part of VS7. VS7 also supports VB,VC and markup language--vbscript and JScript. All of these languages
will be supported in next Generation Windows Services (NWGS) platform (C # requires a
Nwgs SDK packs can be downloaded on m$ 's website. With this dongdong (NWGS), C # does not need its own class library,
and use VC or VB such some mature library. C # also does not have its own class library.
The nonsense is over.

1. 11 Old Fashioned examples (can't you change it?) )*/
/* IdontlikeHelloworld.cs:such A out sample:(* *
1:using System;
2:class Idontlikehelloworld
3: {
4:static void Main () {
5:console.writeline ("I dont like Hello world");
6:console.readline ();
7:}
8:}
* * If I want to give a book, I will consider a better example. ^&^

Let's talk about how to run. First, you need windows2000!. (yes, that's it, please don't throw the peel--
I can have the whole banana left. Then, need Nwgs sdk! (82.4MB, not very big.) Hey, fortunately
It has no class library of its own. After installation, type in the same directory as your program:

csc IdontlikeHelloworld.cs (plus one enter key)

Is it a little retro flavor? This operation will produce a *.cs in the same directory as your
IdontlikeHelloworld.exe files. Double-click it, and the distance can be seen:

I dont like Hello world

A carriage return can end it, very simple. However, you can also do this: save it as a file with a suffix of. C Better
(ie: IDONTLIKEHELLOWORLD.C). So you can use the VC IDE for typing, editing. VC's
TXT Editor is the best Oh (and Vc,no!!! )。 And then:

csc idontlikehelloworld.c (plus one enter key)

The final effect is exactly the same. OK, now parse the syntax: (C # is completely grammatically:-| )

1:using System;

Using is actually a C + + keyword, in C # meaning is similar (that is, I dare not 100% sure, sorry). Using
Used after another keyword namespace. Or look at the namespace first.
Syntax (syntax): (from MSDN)

namespace [identifier] {namespace-body}

I understand:
Identifier: Here is System (remember: C # is the same as C + +, is case-sensitive!) )。 System
Must be unique within the scope in which it is used. That is, you cannot have a second system, but you can have system.
and "Its scope", I do not want to explain in detail, only in practice can grasp. Moreover, beginners simply
No need to know! I also recently learned that there is a namespace and using. :)

In {namespace-body} is really useful dongdong, including the fifth line of "Console.WriteLine" Declaration and
Definition (which is also mentioned later). System is defined by NWGS, we simply use it. As for system in
What file definition, we don't care! Give it to the compiler (just the "csc.exe") to look for it. This is instead
The "#include" in C + +, can be said to be a step closer to avoid a lot of annoying details. If you have not studied C + +,
Just ignore it. Namespace is going to talk about it later.

2:class Idontlikehelloworld

Class: Is another keyword in the C language category. Represents a series of attributes (official: attributes) and behavioral methods that have
It your program can be "alternative" to create and do not have your own characteristics of the different Oh! Here, I have defined
"Idontlikehelloworld". Note: This is also mandatory for C #, and must be available for every executable program. You want to do
Can be recorded in a pair of curly braces that follow the class you define. Note: "{" and "}" one by one correspond,
"(" and ")" the same.

4:static void Main () {

Main () is the first action (behavior method) of this example, the first thing to do. It belongs to the Idontlikehelloworld I define
The method of the class. and is the C # mandatory, is the real beginning of the program! The order of the statements in the "{}" immediately following it is the program
Order of Operation! In this case, there is only one line (what's the sixth line for?) You can get rid of it and compile it one more time, and output a sentence.

5:console.writeline ("I dont like Hello world");

Very strange, Console (again: note the case) is not my definition, from where? It actually belongs to the system
A class of namespace. WriteLine () is a method in the console class to display a sentence (a string).
Here is only 1/18 of this method! And is one of the simplest! Others have a chance to say. You can also use
"Console.WriteLine" Searches "Console.WriteLine" in "Ngws SDK documentaion", remember to check
Search title only, it lists 19 items. All right, it's over! Actually, there's "." I didn't say it! Oh ... lei si la!!!!
(continued)
“.” called the separator (separator), used to connect the name, as above "Console.WriteLine", to put the class and its
Method connection. In this way, we can use the ready-made method collection. Here is a review of my example to see namespace and
“.” is how to use it, and why you should be using the namespace keyword. Change the example slightly: * *
* IdontlikeHelloworld.cs * *
1://using System;
2:class Idontlikehelloworld
3: {
4:static void Main () {
5:system.console.writeline ("I dont like Hello world");
6:system.console.readline ();
7:}
8:}

/* See, when I log off "using System;" , the "System" is added to lines fifth and sixth. The results of the program will not change
Change. However, it is obvious that this is more verbose, so the introduction of the "namespace". In fact, class should be able to accomplish the same function.
However, the designer may not want a keyword to cover too many features. Remember when C to C + +, the introduction of the "Class", and
is not a feature that extends the "struct" keyword, or "=" is used only for assignment, and "= =" is used only to determine equality. This is C/s + + and C #
One of the important features of grammar. The benefits of this design are many. We'll have a chance to talk.
If you have not studied C + +, the following content can be skipped. There are grammatical differences between C # and C + +, such as:
1. C # has no "::"; "->" is only a small fragment in the program. You should use "." In C #.
2. C # does not need to declare a definition before using it. Same as Java.
3. C # cancels the use of "#include" to import other program text files and introduces other people's code with a symbolic handle. So
Eliminates the barriers between programming languages and facilitates the use of libraries written in other languages. such as the "Console" class can be C # or written in any other language.

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.