C # Study Notes (1)

Source: Internet
Author: User
Tags array definition case statement

C # Study Notes (1): C # differs from C ++ syntax

1, C # does not have the ":" domain operator, all use. However, ":", C # ":" is used as the modifier of The namespace alias.

2. Variables in C # Must be initialized before they are used,ProgramExplicitly or automatically by the compiler. Where: The variable is a field in the class or structure. If no Explicit initialization is performed, the value is 0 when these variables are created by default. However, the local variables of the method must beCode

Explicit initialization. (This is different from C ++ ).

3. For example, for the following statement: sometype OBJ; in C ++, This is a value type, and a sometype instance will be created in the stack. But in C #, this will only create a reference for the sometype object, and this reference does not point to any object. You cannot call its method. However, the new keyword must be used to instantiate objects in C # To store objects on the stack. Returns a reference.

 

4. The following code:
Public static in Mian ()
{
Int J = 20;
For (INT I = 0; I <10; I ++)
{
Int J = 30;
//... Use value J...
}
}
Different from C ++, C # cannot hide the variable for the for loop at this time: J. The preceding J is still in the definition field. But for the following code:
Static Int J = 20;
Public static void main ()
{
Int J = 30;
//... Use J
}
This is acceptable. This is a static variable, but note that instance variables of the class cannot be used in static methods.

5. constants in C # are always static and cannot be added before the declaration of constants. In C #, whether a variable is a value or a reference only depends on the Data Type of the variable. Int is always a value type (unless the packing technology is used ). However, in C ++, we can directly choose whether it is a value or a pointer to access a variable. Note: string and object are reference types.

6. The custom struct type in C # is a value type, and the custom Classe type is a reference type. C ++ does not have this difference.

In 7, C #, the byte type is unsigned by default, and the signed version is sbyte.

 

8. Several floating point types:
Float Type: The CTS type is system. Single, 32-Bit Single-precision floating point number, and the number of decimal places is 7 digits. The range is roughly: (+/-) 1.5*10 ^ (-45 )~ (+/-) 3.4*10 ^ 38.
Double Type: CTS type: system. Double, 64-bit double-precision floating point number, 15/16 digits after the decimal point, range: (+/-) 5.0*10 ^ (-324 )~ (+/-) 1.7*10 ^ 308.
Decimal type: CTS type: system. decimal, 128-bit high-precision decimal notation. The number of decimal places is 28, (+/-) 1.0*10 ^ (-28 )~ (+/-) 7.9*10 ^ 28, mostly used for financial computing. Demical is not a basic type and may cause performance loss. Decimal d = 12.30 m;

The char type of C # Is a 16-bit (UNICODE) character.

 

10. Notes about the string type: although the string type is a reference type, there are many differences.
For example, string S1 = "AAA"; string S2 = S1; S1 = "CCC"; S2 does not change with S1, which is different from the reference type.
You can add the @ symbol before the string literal: the output is the content in. Even if there is a carriage return, the line feed will be:
@ "AA \ B" is equivalent to "AA \ B"
@ "'Asdasdfa A aggagag
Fafafa DFA "output:
Asdasdfa A aggagag
Fafafa DFA

 

11. In the switch... case... statement, the case must be followed by a constant expression (including a String constant) or enumeration type. However, note that, unless the case statement is followed by a null handler, you must add a break statement. If the case clause is empty, you can skip to the next statement. You can also use the GOTO statement to skip. (The jump of the GOTO statement in the switch... case... statement in C # is a useful place ).
Case "America ":
Do ....;
Goto case "Britain ";
Case "France ":
Do ....
Break;
Case "Britain ":
Do...
Break;
This is also possible.

12. The values of items in a foreach loop cannot be changed.

 

13. The array definition of C # Is: int [] integers = new int [32];
All arrays belong to the reference type and follow the reference semantics. Each element of the array can be a value type. Int [] Copy = integers; indicates that copy points to the same array instead of creating a new one.

 

14. Formatting output of console. writeline:
Int I = 900, j = 80;
Console. writeline ("{0} plus {1} equals {2}", I, j, I + J );
You can also specify the width of the value to adjust the position of the text in the width. The positive value is right aligned, and the negative value is left aligned. In the format of {n, w}, n is the index parameter, and W is the width value.
Console. writeline ("{0, 4} \ n + {1, 4} \ n ---- \ n {2, 4}", I, j, I + J );

15. Adding @ in front of Reserved Words in C # indicates an identifier.

16. The string type cannot change the value of the string (if it is changed, a new string will be created in the heap), so it is different from the general reference. Therefore, the real parameter cannot be changed when the string is used as the parameter.

 

17. Differences between ref and out:
Ref: the parameter of the value type can be changed to the reference type. Void fun (INT [] ints, ref int I) also needs this keyword fun (ints, ref I ). Note: Both value passing and reference passing must be initialized.
Out: If the parameter is prefixed with out, the variables passed to the method can be not initialized. However, you must assign a value to this parameter in the function, and then initialize the real parameter.

 

18, C # overload:
(1). Default real parameters are not supported.
(2) The two methods cannot be different based on the return type.
(3) The two methods cannot be distinguished based on whether the parameter is declared as ref or out.

 

19. Static constructor:
(1) A Class (not necessarily a static class) can write a static constructor, but it cannot have any parameters.
(2) The static constructor consists. net call, the programmer cannot determine at which exact time it calls, but it must be called before the first time the program uses the class, only once, if the static variables of the class have the default value, it must be specified before calling the static constructor.
(3) A good use of the static constructor is to initialize static fields or attributes. Including constants or read-only members.

 

20. The C # constructor can only call Constructor (this () can be used) and Cannot initialize variables.

21. Read-Only variable: readonly
Readonly allows a field to be set as a constant, but some operations can be performed to determine its initial value. You can only assign values to the constructor (initialization can be performed when the constructor is declared), but not elsewhere. The read-only field can also be an instance variable (the constant const can only be a static member by default), initialized in the instance constructor, so that each instance of the class can have different values. Set the read-only field to the declaration that must be displayed statically. We do not need to set read-only fields or constants to private because they cannot be modified externally. If no value is assigned to a read-only field in the constructor, it will also be the default value of its data type, or the initialization value during the declaration.

 

22, the difference between struct and class:
(1), the structure is a value type, and the class is a reference type. The structure is stored in the stack or is stored in inline mode, and its lifecycle is consistent with that of a simple data type.
(2), the structure does not support inheritance, but it is derived from vlauetype, that is, the derived class of the object class. It can inherit or reload its method.
(3) the constructor of the structure works differently from the class. The structure is always a default constructor without parameters provided by the compiler, which cannot be replaced by the user, that is to say, you can only define constructors with parameters.
(4). You can specify the Field Layout in the structure using the structure.
(5), for example:
dimensions point = new dimensions (); point. length = 3; here, the new operator is different from the class type. It does not allocate memory space in the heap, but calls the corresponding constructor according to the corresponding parameters.
however, for: Demensions point; double D = point. length; it will not work, because it only allocates the memory space of the variable point in the stack, but does not initialize its members, just as the uninitialized members are not accessible (can be assigned a value ).
(6), the variable defined in the structure cannot be initialized during Declaration: struct dimensions {public double length = 1; int width = 2;} is incorrect.

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.