Chap02 C # BASIC language knowledge

Source: Internet
Author: User

Environment

Visual Studio 2010

. NET Framework 4.0

1. Compilation & Disassembly

1.1 compile csc.exe/out: C: \ test.exe c: \ test. CS

1.2 use the Il Disassembly tool for disassembly. Open test.exe and store it.

2. Array

Int [,] points = new int [3, 2] {1, 2}, {2, 3}, {3, 4 }};

If no value is assigned, the default value is 0 (null in the book ).

3. type conversion

Int A = int. parse ("3 ");
Int B = (INT) 3.14;
Int c = system. Convert. toint32 (3.14 );
Float d = system. Convert. tosingle ("3.14 ");
String E = 3.14.tostring ();
Console. writeline ("A = {0}, B = {1}, c = {2}, D = {3}, E = {4}", a, B, c, d, e );

Output: A = 3, B = 3, C = 3, D = 3.14, E = 3.14

4. Comment

4.1 //,///

4.2 /**/

4.3

# Region introdoction

...

# Endregion

5. Keyword parsing (partial)

5.1 foreach clause

The foreach statement is used to traverse all elements in the set.InterfaceSupported

Int [] array = new int [] {1, 2, 3 };

Foreach (INT item in array)

Console. writeline (item );

5.2 lock statement

The lock keyword marks the statement block as a critical section to obtain the mutex lock of a given object, execute the statement, and then release the lock.

5.3 checked & unchecked

The keyword is used to cancel overflow checks for integer arithmetic operations and conversions.
Const int constantmax = int. maxvalue;

Int1 = unchecked (constantmax + 10); // No error is reported

5.4 using

Using (streamwriter Sw = file.createtext('test.txt '))

{

Sw. writeline ("Line 1 ");

Sw. writeline ("Line 2 ");

}

5.5 yield

Static ienumerable range (int from, int)

{

For (INT I = from; I <to; I ++)

Yield return I;

}

Static void main ()

{

Foreach (INT item in range (-5, 5)

Console. Write (item + ",");

}

5.6 unsafe and fixed

Static int x = 2;

Unsafe static void F (int * P)

{* P = 1 ;}

Static void main ()

{

Unsafe

{

Fixed (int * P = & X) f (p );

Console. writeline (X );

}

}

The memory to which the pointer points must be fixed. All the reference types in C # (all types of arrays are reference types) are allocated on the managed stack and are not fixed. There are two ways to force fixation: one is to use stackalloc to allocate on the stack, and the other is to use fixed to allocate on the stack.

5.7 typeof

The system. Type object used to obtain the type. The typeof expression is in the following format:

System. Type type = typeof (INT );

To obtain the runtime type of the expression, you can use the. NET Framework Method GetType, as shown in the following example:

Int I = 0;

System. Type type = I. GetType ();

5.8 lambda expressions

Delegate int del (int I, Int J );

Static void main (string [] ARGs)
{
Del mydelegate = (x, y) => X * Y;
Int J = mydelegate (5, 20 );
Console. writeline (j );

}

5.9 As and is

X is T

If X is t, true is returned; otherwise, false is returned.

X as t

Returns X of the T type. If X is not t, null is returned.

5.10 ?? 

Null merge

X ?? Y

If X is null, the calculation result is Y. Otherwise, the calculation result is X.

5.11 new

The new operator is used to create objects on the stack and call constructors. It is worth noting that value-type objects (such as structures) are created on the stack, and reference type objects (such as classes) is created on the stack.

 

 

6. Exception check

Throw, try-catch, try-finally, try-catch-finally


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.