C # basics of programming C #

Source: Internet
Author: User

C # The code looks and operates in a similar way as C ++ and Java. At the beginning, it seems that its syntax may be chaotic, unlike written English and other languages. However, in C # programming, the style used is relatively clear, and you do not have to spend too much effort to compile highly readable code.

Unlike compilers in other languages, C # compiler does not consider space, carriage return, or tab characters (these characters are collectively referred to as blank characters. This gives you a lot of freedom to format the code, but following some rules will help make the code easy to read.

C # The Code consists of a series of statements. Each statement ends with a semicolon. Because space is ignored, a line can have multiple statements, but from the readability point of view, it is usually followed by a carriage return character, so that multiple statements cannot be placed on a line. However, it is possible to put a sentence of code on multiple rows (which is also common ).

C # Is a block structure language. All statements are part of a code block. These blocks are defined in curly brackets ("{" and "}"). A code block can contain any number of statements, or it does not contain statements at all. Note that the curly braces do not require a semicolon.

Therefore, the simple C # code block is as follows:

{
<Code line 1, Statement 1>;
<Code line 2, Statement 2>
<Code line 3, Statement 2>;
}

The <code line x, statement y> part is not really C # code, but uses this text as a placeholder for the C # statement. Note that in this Code, lines 2nd and 3 are part of the same statement, because there is no semicolon at the end of line 2nd.

In this simple code block, the indent format is also used to make the C # code more readable. This is not my invention, but a standard rule. In fact, by default, Vs will automatically indent the code. Generally, each code block has its own indentation level, that is, how much indent it has to the right. Code blocks can be nested with each other (blocks can contain other blocks), while nested blocks are more indented.

{
<Code line 1>;
{
<Code line 2>;
<Code line 3>;
}
<Code line 4>;
}

The previous code is usually more indented, such as the 3rd line of code in the first example above.

Note:

In the vs Options dialog box that can be accessed through tools | options, the vs rules used to format the code are displayed. The sub-directory of the text editor | C # | formatting node contains the complete formatting rules. Most of the settings here reflect the C # section that is not described yet, but if you want to modify the settings later to better suit your personalized style, you can look back at these settings. In this book, for the sake of simplicity, all the code segments are formatted using the default settings.

Remember, this style is not mandatory. However, if you do not use it, readers will soon become confused when reading this book.

In C # code, another common statement is annotation. Annotations are not strictly C # code, but it is best to annotate the code. Annotations are explanations, that is, to add descriptive text (in English, French, German, and Mongolian) to the code. The Compiler ignores the content. When you start to process long code segments, annotations can be used to add prompts to ongoing work. For example, "this line of code requires users to enter a number" or "this code is written by Bob ". C # There are two ways to add annotations. You can place a tag at the beginning and end of the annotation, or use a tag, which means "the rest of this line of code is annotation ". In the C # Compiler's rule that ignores carriage returns, the latter is an exception, but this is a special case.

To mark a comment in the first way, you can add "/*" at the beginning of the comment and "*/" at the end. These annotation symbols can be on a single line, or on different lines. All the contents between the annotation symbols are comments. The only comment that cannot be entered is "*/", because it is regarded as the end mark of the comment. Therefore, the following statements are correct.

/* This is a comment */
/* And so...
... Is this! */

However, the following statement produces an error:

/* Comments often end with "*/" characters */

The content after the end symbol is commented (the character after "*/") will be treated as C # code, so an error is generated.

Another way to add a comment is to start a comment with "//", and then you can write any content as long as the content is on one line. The following statement is correct:

// This is a different sort of comment.

But the following statement will fail, because the second line of code will be interpreted as C # code:

// So is this,

But this bit isn' t.

Such annotations can be used to describe statements because they are all placed on one line:

<A statement>; // explanation of statement

As mentioned above, there are two ways to add comments to the C # code. However, in C #, there is a third comment. Strictly speaking, this is an extension of the // syntax. They are all single-line comments starting with three "/" symbols, rather than two.

/// A special comment

Under normal circumstances, the compiler ignores them, just like other annotations, but vs can be configured to extract the text after these annotations during project compilation, create a text file in special format that can be used to create a document manual. For details, see Chapter 1.

Note that the C # code is case sensitive. Different from other languages, you must enter the code in the correct case format, because simply replacing lowercase letters with uppercase letters will interrupt project compilation.

If the reader has no knowledge of C # language, it is difficult to understand this. Let's look at the following line of code, which is used in the first example in Chapter 2nd:

Console. writeline ("the first app in beginning C # programming! ");

C # the compiler can understand this line of code, because the console. writeline () command is in the correct case format. However, none of the following statements can work:

Console. writeline ("the first app in beginning C # programming! ");

Console. writeline ("the first app in beginning C # programming! ");

Console. writeline ("the first app in beginning C # programming! ");

The case format used here is incorrect, so the C # compiler does not know what to do.

Fortunately, vs provides a lot of help in code input. In most cases, it knows (the program knows) What we are going to do. During code input, vs recommends the commands that users may want to use and tries to correct the case.
C # basic structure of console applications

Let's take a look at the console application example (consoleapplication1) in Chapter 1 and study its structure. The Code is as follows:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
// Output text to the screen.
Console. writeline ("the first app in beginning C # programming! ");
Console. readkey ();
}
}
}

As you can see, all the syntax elements discussed in the previous section are available here. There are semicolons, curly braces, comments, and appropriate indentation.

Currently, the most important part of the code is as follows:

Static void main (string [] ARGs)
{
// Output text to the screen.
Console. writeline ("the first app in beginning C # programming! ");
Console. readkey ();
}

When running the console application, run this code. More accurately, it is the code block in curly braces. As mentioned above, the Comment lines do not do anything, including them only for simplicity. The other two lines of code output some text in the console window and wait for a response. But at present, we do not need to care about its specific mechanism.

Note how to implement the Code highlight function described in the previous chapter. Although this is important for Windows applications, it is a very useful feature. To implement this function, you need to use the # region and # endregion keywords to define the beginning and end of the Code area that can be extended and shrunk. For example, you can modify the code generated by leleapplication1 as follows:

# Region using directives
Using system;
Using system. Collections. Generic;
Using system. text;
# Endregion

In this way, the code lines can be reduced to one line. You can extend the code lines again when you want to view the details later. The Using statement and its namespace statement are described later in this chapter.

Note:

Any keyword starting with # is actually a pre-processing command, strictly speaking, it is not a C # keyword. In addition to the # region and # endregion keywords described here, other keywords are quite complex and professional in usage. Therefore, this is a topic that readers can explore only after reading the book.

You do not need to consider other code in the example, because the first few chapters in this book only explain the basic syntax of C #, as for the Application console. the specific method of calling writeline () is beyond our consideration. The importance of these codes will be elaborated in the future.
 

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.