C # essential syntax in getting started (1 ),

Source: Internet
Author: User

C # essential syntax in getting started (1 ),

First, we need to know that C # is an object-oriented language evolved from C and C ++, which relies on. NET Framework.. NET Framework provides a powerful code library for its calling. C # Language depends on. NET Framework because C # source code is only compiled by the compiler (VS or VCE) and becomes a Common Intermediate Language. Generally, the. exe or. dll file we use is also called an assembly. When we run the .exe file, the Assembly will be translated into CPU commands by the CLR-Common Language Runtime and then handed over to the CPU. Therefore, we use C # To write only the code in the hosted environment (subject to CLR control ). Those who have learned JAVA can regard CLR as a JAVA virtual machine, mainly responsible for memory management, processing security, garbage collection, and cross-language debugging.

To learn a programming language, we also need to understand its basic syntax: For the syntax, you only need to have a book about C # language.

1. Display conversion and implicit conversion

Implicit conversion: Type A to B can be performed in all cases, and the compiler performs conversion independently. So when will implicit conversion be performed? -As long as the value range of type A isCompleteValues included in value B can be implicitly converted.

 

1 char str = 'a'; 2 ushort newStr; 3 newStr = str; 4 Console. writeLine ("str {0}", str); 5 Console. writeLine ("newStr {0}", newStr );Implicit conversion

 

Output result: str

NewStr 97

In this case, the compiler automatically performs implicit conversion. Values of ushort and char types are in the range of 0 ~ Between 65535, so you can perform type conversion.

Explicit conversions: Microsoft provides us with Convert methods in the program ). But pay attention to the overflow problem. If the Type A value to be converted exceeds the value range of type B, it will overflow, but the system will not report an error by default, but it will cause data loss. The chack method can be used for check.

Short str = 285; byte newStr; newStr = Convert. toByte (str); Console. writeLine ("str {0}", str); Console. writeLine ("newStr {0}", newStr );Explicit Conversion

The value is too large or too small for unsigned bytes.

2. Parameter issues in Functions

First, pay attention to the fact parameter and the form parameter must be of the type match when the function is called.

C # Allow one (or only one) specific parameter to be specified. It must be the last of the parameters and be modified using params, which is called a parameter array.

Static int sumVals (params int [] vals) {int sum = 0; foreach (int val in vals) {sum + = val;} return sum ;}Summation Function

Reference parameters ref and out

Ref is characterized by inbound and outbound, that is, it has been assigned a value before passing the parameter. When passing in the method body, the address of this number is passed in, if you assign values to them, the value in the address is directly changed. Therefore, when the method is executed, the value of this number is changed. The only difference between out and ref is that after the method receives a parameter, It is initialized (if there is no initialization, an error will be reported), and the remaining usage is the same as that of ref.

The ref keyword allows the parameter to be passed by reference. When a method is called, any changes to the parameter in the method will be reflected in the variable. C ++ can be referenced by analogy.

Class Ref {static void refExample (ref int I) {I = 20;} static void Main () {int val = 0; refExample (ref val); Console. writeLine (val); // print 20 at this time }}Reference parameters

 

Static void Main (string [] args) {int I; Add (out I); Console. writeLine (I); // result output 11} static void Add (out int I) {I = 10; // if no value is assigned, the error I = I + 1;} will be reported ;}Ref and out are different.

 

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.