Starting from the keyboard layout, the symbols in C #

Source: Internet
Author: User
ArticleDirectory
    • Single symbol
    • Symbol combination

There are various summary and classification articles everywhere. Therefore, I will also take readers to summarize the symbols that appear in. net. I hope you can clarify the vague memories of some symbols and give some inspiration to you.

ProgramTake five seconds to move your eyes away from the screen and look at your keyboard (e.g. figure 1) look for the familiar symbols on the keyboard. As a C # developer, have you suddenly discovered that most of them are so friendly? Yes. Most of the symbols on the keyboard have been used by Microsoft. Although the remaining symbols have not been used yet, I believe someone inside Microsoft should also stare at them, maybe one day you will find that a new symbolic usage will be added to C. Because symbols are often more expressive than letters in a language, they also impress developers, the most common examples are the +/-/* // mathematical operators ,. net also supports heavy-duty common operators. Now, I will explain these symbols from left to right based on the keyboard layout. By the way, I will also take you to review these symbols.

Figure 1. keyboard symbol

Single symbol

In C #, these symbols are not only used separately, but also used in combination. Therefore, we will first introduce their usage.

1 .~

The first is to evaluate the population by bit. This symbol may be rarely seen by some students, but it is indeed useful. The 32-bit expression of e.g. 1 is0000 0000 0000 0000 0000 0000 0000 0001, so int A = ~ 1 is actually-2, that is1111 1111 1111 1111 1111 1111 1111. In addition ,~ In list binarysearch, your operations will be much easier. In the implementation of this binary search, if the searched element is not in the Set, it returns a negative value, and the technique is here, the result of this number is the position of the next element greater than the search element. If there is no larger element, it indicates the total number of elements. This technique makes it much easier to insert new elements. E.g.

Code

  Static     Void  Main ()
{
List < String > List = New List < String > ();
Int Search;

List. Add ( " Public " );
List. Add ( " Protected " );
List. Add ( " Private " );
List. Sort ();
Search = List. binarysearch ( " Protected internal " );
If (Search < 0 )
{
List. insert ( ~ Search, " Protected internal " );
}
Foreach ( String Accessmodifier In List)
{
Console. writeline (accessmodifier );
}
}

Output result:

 
Private
Protected
Protected internal
Public

The other one that we use is less and less destructor now. Add ~ before the constructor ~ It is equivalent to the operation that tells CLR that you need to release an object when it is deregistered. Pay attention to the following points for destructor:

    • The Destructor cannot be defined in the structure. Only destructor can be used for classes.

    • A class can have only one destructor.

    • The Destructor cannot be inherited or overloaded.

    • You cannot call the destructor. They are automatically called.

    • The Destructor neither have modifiers nor parameters.

The following is a simple example:

  ClassCar
{
~Car ()//Destructor
{
//Add Resource Release OperationCode
}
}

However, we recommend that you use the Destructor as few as possible and use the idispose interface to release resources.

2 .!, @, # And $

! I believe everyone on Earth knows that non-logical operators are not mentioned here. @ Is often used at the beginning of a string. It tells the compiler to ignore the escape character, so that some special characters can be conveniently output according to the user's wishes, and in the Aspx.. net. # And $ are not targeted at Microsoft for the moment (incorrect expression, which will be added later ). However, I believe many people are familiar with the $ symbol. The famous jquery makes this symbol visible everywhere, but it is not guaranteed that Microsoft will pin it in the future.

4. %, ^, &, *, (),-, +, =, {}, | and \

These symbols are common symbols. Therefore, we will only briefly explain them here. % Is the remainder operator, ^ is the XOR operator, & is the and bitwise operator, and | is or bitwise operator. * It can be used as a multiplication symbol or a pointer. () And {} are used in objects. The former indicates the function parameter area, and the former indicates that a type is forcibly converted to the type in brackets during forced type conversion. In addition, it is also used to wrap the operation part to set the operation priority. The latter indicates the code segment scope. It can be used to refer to the classes or classes in the namespace, including the attribute methods, it can also be used to specify the blocks, such as fixed and using, when assisting some special operations in C #. In addition, in string formatting, {} and the combination of digits indicates the position of the substring to be replaced in the target string, e.g. string. format ("{0} + {1} = {3}", 1, 2, 3 ). + It usually indicates the addition math operator. If it is equal to "=", it indicates the value assignment operation. In addition, these operators can be combined to indicate different meanings, which will be described in detail later. \ Is used to start the Escape Character. For example, \ n indicates a line break.

5. []

This symbol is also common. It is usually used in three ways. One is used to represent the index position in an array and the other is used to add a type, in addition, it is often used with the this keyword in the class to form a class index. E.g.

 Class  Some
{
Int This [ String Item]
{
Get
{
If (Item = "") Return 1 ;
Return 0 ;
}
}
}

6.:,; ', ",', <,> ,.,/,?

: Currently, there is no separate use case. We will talk about its combination and usage later. It can be seen everywhere. C # is not like VB, and it is used; it indicates the end of the statement ." Package string, 'is wrapped in a single character. <And> are logical operators, which are used to separate parameters. They are used to represent the object member selector or decimal point. /Indicates the mathematical operator divided .? A single value type is commonly used to indicate null values, e.g. Int? A = NULL, which can be combined with many characters to express different meanings.

Symbol combination

Now, the introduction of the symbol keys on the keyboard is complete. in C #, the more powerful side of the symbol is that different combinations of symbols have different meanings. Here we will give a brief introduction to this.

1. Duplicate combinations of a single character

Here is? And +,-, &, |, <,> and = .?? It is also used in generic data operations. It can be used to specify the default value when the value type is null. e.g. int A = somenullable ?? 100; it indicates that if somenullable is not empty, the value is assigned to a; otherwise, 100 is assigned to a. You can also refer to this article. ++ And -- indicate auto-increment or minus 1. &, | And = indicates the logical relationship and, or equal. <And> indicates the left-right shift operation. // Represents a single line comment, O (partition _ comment) O ~.

1. mathematical operators, bitwise operators, and combinations of logical operators and =.

Similar to C/C ++, C # simplifies the input of programmers in common mathematical operations by combining these two symbols. For example, + =,-=, * =,/=, % =, and & = ,~ =, | =, Etc. It is the result of a mathematical operation between a number itself and a number. For example, the complete form of A + = 10 is: a = a + 10. The relationship between logical operators and = indicates or, for example,> = indicates greater than or equal.

2 .? And: Combination

The symbols of this combination are not closely related, but they must be used together. Here? And: form a conditional expression, e.g. Int c = A = B? A: B; its existence also simplifies programmer code.

3. => combination and <> pairing

=> The combination is added after 3.0. It also forms the Lamda expression, which makes the writing of anonymous functions more concise. Usually it contains the expression parameter section, followed by the body section of the expression. <> It is widely used in generics and is used to wrap type parameters.

4. symbol combinations on the ASPX page

There are many combinations of symbols in Aspx. Generally, the combination of left and right angle brackets and other symbols indicates the script code on the server, so as to insert the server code. Only common symbol combinations are listed here.

First, <% @ and %> are used to indicate the page command of aspx 2.0, which usually appears at the top of the page. On the Aspx. NET page or user control, there are 11 commands (Note 1) in the format of <% @ [directive] [attribute = value] %>.

<% = %> Used to insert. Net code snippets into HTML code. In Aspx. Net MVC, this character combination is very common, similar to the previous ASP, which is also one reason why some Programmers think MVC is returned to the ASP era.

Summary

In many cases, a symbol not only expresses its meaning more concisely than a character, but also is more vivid and intuitive. This is also a reason why the symbol appears in. net. However, there are not too many symbols, the better. The so-called too many symbols are too complicated. Sometimes, it may lead to confusion and misunderstanding for programmers. Therefore, the number of symbols in. NET is not too large, and in the future, the symbols in. net should be basically fixed. However, this does not prevent Microsoft from adding some new and useful symbols when appropriate. For example, someone is looking forward to it. net 5.0 mentioned a very good feature of Microsoft's Vedea language: binding. Its symbol is textbox. text: = slider. value; this means that when the value changes, the text will also change accordingly. The two-way binding syntax is different: textbox. text: =: slider. value; believe that this new symbol is added. net will certainly make our code more convenient than before! At the same time, we also look forward to similar new features that can simplify our programmers and make images more visible in the future. By the way, what new symbols do you expect to add?

Appendix:

Thank you very much for the summary of the addition of Arthur-Cui for more usage of the C # symbol.Here I will add them.

1 ,@

Sometimes we may need some.. Net keyword variables with the same name to more accurately express the meaning of the variable, but the keyword is not allowed in C #, this time @ can be used. E.g.

  Public Static VoidSomemethod (ThisForm @ this)
{
StringA=@ This. text;
}

Here, if you use reflector to view the Il variable, you will find that it becomes this, but it is still recognized as @ this in the compiler. If sometimes we really need this scenario, it will be helpful to your code.

2 ,#

Sometimes, after processing common exceptions, we may often add a catch to catch all other unprocessed exception information. However, this catch may not write the exception variable, e.g.Catch {// exception handling program }. So how to obtain the exception information. At this timeYou only need to enter $ exception in the real-time window or monitoring window.

About # A very common and frequently used scenario, Aras-Cui also adds that it divides code blocks and Conditional compilation. E.g. 

  # If
# Else
# Define
# Region
# Endregion

3, $

$ Symbol is secretly used by the compiler in the anonymous method. You can view the Il code to understand its name.However, this method is invisible to programmers. The private method generated by the machine is not displayed in intelliisense and cannot be called explicitly.


Note 1: Asp. net 11 page or user control commands are: Assembly, control, implements, import, Master, mastertype, outputcache, page, previouspagetype, reference, register.

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.