Introduction to C # Programming classic C # basic syntax

Source: Internet
Author: User
Tags closing tag

C # code looks and works very much like C + + and Java. At first glance, its syntax may be confusing, unlike written English and other languages. However, in C # programming, the use of the style is relatively clear, do not have to spend too much effort to write a very readable code.

Unlike compilers in other languages, the C # compiler does not consider these characters regardless of whether the code has spaces, carriage returns, or tab characters, which are Fu Shi called whitespace characters. This gives you a lot of latitude in formatting your code, but following certain rules will help make your code easier to read.

C # code consists of a series of statements, each of which ends with a semicolon. Because a space is ignored, a row can have multiple statements, but from a readability standpoint, you usually add a carriage return after the semicolon so that you cannot place multiple statements on a single line. But it is possible to put a code on more than one line (and more often).

C # is a block-structured language, and all statements are part of a block of code. These blocks are delimited by curly braces ("{" and "}"), which can contain any number of lines or do not contain statements at all. Note the curly brace character does not need to be accompanied by a semicolon.

So, the simple C # code block looks like this:

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

In this simple code block, indented formatting is also used to make C # code more readable. This is not my invention, but rather a standard rule, which in fact automatically indents the code by default. In general, each code block has its own indentation level, which is how much it indents to the right. Blocks of code can be nested with each other (that is, blocks can contain other blocks), and nested blocks are indented more.

The continuation line of the preceding code is usually indented a bit more, as in the 3rd line of code in the first example above.

Comments:

In the ability to pass Tools | The Options Access vs Options dialog box shows the VS rules for formatting code. In the text Editor | C # | The subdirectory of the formatting node contains the complete formatting rules. Most of the settings here reflect the C # part that is not yet covered, 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 brevity, all code snippets are formatted with the default settings.

Remember, this style is not mandatory. But if you don't use it, readers will quickly fall into confusion as they read the book.

In C # code, another common statement is a comment. Comments are not strictly C # code, but the code is best commented. Annotations are explanations that add descriptive text (in English, French, German, foreign Mongolian, and so on) to the code, and the compiler ignores the content. Comments can be used to add hints to work in progress, such as "This line of code requires the user to enter a number", or "This code is written by Bob," When you start working on a long code snippet. There are two ways to add comments in C #. You can place a tag at the beginning and end of a comment, or you can use a tag that means "the remainder of this line of code is a comment." In the rule that the C # compiler 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 with "*/" at the end. These annotation symbols can be on a single line or on a different line, and all of the content between the comment symbols is a comment. The only thing that cannot be entered in the note is "* *" because it is treated as a comment closing tag. So the following statement is correct.

However, the following statement produces an error:

Note the contents after the end of the symbol (the character after "*/") are treated as C # code, resulting in an error.

Another way to add comments is to start a comment with "//", and then you can write anything, as long as it's on a single line. The following statement is correct:

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

Such annotations can be used for descriptions of statements, because they are all placed on one line:

As mentioned earlier, there are two ways to add comments to C # code. But in C #, there is a third kind of comment, which, strictly speaking, is an extension of//syntax. They are all single-line comments, starting with three "/" symbols instead of two.

Under normal circumstances, the compiler ignores them, just like any other comment, but you can configure VS to extract the text following these comments when compiling the project, creating a specially formatted text file that can be used to create a document specification. See chapter 28th for specific information.

It is particularly important to note that C # code is case-sensitive. Unlike other languages, you must enter the code with the correct capitalization, because simply using uppercase letters instead of lowercase letters interrupts the compilation of the project.

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

The C # compiler can understand this line of code because the Console.WriteLine () command is in the correct case. However, the following statements do not work:

The case used here is wrong, so the C # compiler doesn't know what we're going to do.

Fortunately, VS provides a lot of help with the input of the code, and in most cases it knows what we're going to do. In the process of entering the code, VS will recommend the commands that the user might want to use and correct the case as much as possible.

Basic structure of a C # console application

Let's look at the console application example (ConsoleApplication1) in chapter 2nd to examine its structure. Its code is as follows:

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

At this point, the most important part of the code is as follows:

When you run the console application, you run the code, or more accurately, the block of code that runs in curly braces. As mentioned earlier, the comment lines do nothing and contain them only for brevity. The other two lines of code output some text in the console window and wait for a response. But at the moment we do not need to care about its specific mechanisms.

Note here how to implement the code highlighting feature described in the previous chapter, although this is important for Windows applications, it is a very useful feature. To implement this functionality, you need to use the #region and #endregion keywords to define the beginning and end of the area of code that can be expanded and shrunk. For example, you can modify the code generated for ConsoleApplication1 as follows:

This allows you to shrink the lines of code to a single line and then expand it again later to see its details. The using statement contained here and the namespace statement below it are explained later in this chapter.

Comments:

Any keyword that starts with # is actually a preprocessing instruction, strictly not a C # keyword. In addition to the #region and #endregion keywords described here, other keywords are quite complex and use more professional. So, this is a subject that readers can explore after reading through the book.

It is not necessary to consider the other code in the example, because the first few chapters of this book explain only the basic syntax of C #, and the specific way that the application makes Console.WriteLine () calls is not in our consideration. The importance of the code will be explained later.

C # Programming Primer basic Syntax for C #

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.