C # And. NET2.0 learning notes C # BASIC Language concepts

Source: Internet
Author: User
Tags case statement

I. Pre-processing symbols and Conditional compilation

A) You can use the # define command to define some symbols guiding the pre-processor to modify the source code. This is done by the # if, # elif, # else, # endif command.

# Define MACRO1

Using System;

Public class Program {

Public static void Main (){

# If (MACRO1)

Console. WriteLine ("MACRO1 is defined .");

# Elif (MACRO2)

Console. WriteLine ("MACRO2 is defined and MACRO1 is not defined .");

# Else

Console. WriteLine ("MACRO2 and MACRO1 are both not defined .");

# Endif

}

}

B) You can use the # undef command to remove the definition of the previous symbol.

C) pre-processing symbols and condition attribute. ConditionalAttribute allows a method to be defined based on the value of symbol constants.

# Define _ TRACE __

Class Program {

[System. Diagnostics. Conditional ("_ TRACE _")]

Public static void Trace (string s ){

System. Console. WriteLine (s );

}

Static void Main (){

Trace ("Hello ");

System. Console. WriteLine ("Bye ");

}

}

// If the _ trace _ symbol is not defined, the BYE is displayed. If the _ trace _ symbol is defined, the hello, bye characters are displayed.

 

D) # error and # warning commands.

# The error command will cause a custom compilation error, which is usually used to prevent conflicting symbol definitions during compilation.

# Define MACRO1

# Define MACRO1

# Define MACRO2

# If MACRO1 & MACRO2

# Error MACRO1 and MACRO2 cannot be defined at the same time

# Endif

Public class Program {

Public static void Main (){

# If (MACRO1)

Console. WriteLine ("MACRO1 is defined .");

# Elif (MACRO2)

Console. WriteLine ("MACRO2 is defined and MACRO1 is not defined .");

# Else

Console. WriteLine ("MACRO2 and MACRO1 are both not defined .");

# Endif

 

}

}

E) # paragma warning disable command and # paragma warning restore command. Some specific warning information generated by the C # compiler can be temporarily disabled.

F) # line command, used to change the line number of the compilation error or warning message source.

Class Program {

Public static void Main (){

# Line 1 "Method Main ()"

Int I = 0; // <-ERROR: Not allowed to use the operator? = Bytes?

// In this context!

}

}

The above Code indicates that the error is in the first line.

G) # region and # endregion commands

You can collapse or expand a specific code segment with a single mouse.

Ii. Alias Mechanism

A) You can use the using keyword to define aliases for namespaces or types.

Using C = System. Console;

Class Program {

Static void Main (){

C. WriteLine ("Hello 1 ");

}

}

 

B) Global qualifier

C #2.0 introduces the concept of global delimiters. When a namespace alias delimiters appear before it, it indicates that the compiler needs a namespace name.

Using System;

Class Program {

Class System {}

Const int Console = 691;

Static void Main (){

Global: System. Console. WriteLine ("Hello 1 ");

Global: System. Console. WriteLine ("Hello 2 ");

}

}

C) External alias: The External alias can be defined in two different sets, but the type name and namespace are the same. This can happen if we have to use two different versions of the same assembly. See the following example:

1. Set the alias of Asm1.dll to AliasAsm1 in the reference window when introducing the assembly.

2. Set the alias of Asm2.dll to AliasAsm2 in the reference window when introducing the assembly.

3. Set extern alias AliasAsm1 and extern alias AliasAsm2 in the Code;

The details are as follows:

Extern alias AliasAsm1;

Extern alias AliasAsm2;

Class Program {

Static void Main (){

AliasAsm1: FooIO. Stream stream1 = new AliasAsm1: FooIO. Stream ();

AliasAsm2: FooIO. Stream stream2 = new AliasAsm2: FooIO. Stream ();

}

}

Iii. Notes and automatic documentation

A) Note: Use // and // to annotate the program, while /*... */Used to temporarily block code

B) C # There is an option to generate comments from the source code // annotations.

C) use Ndoc2005 to generate HTML documents for XML documents

Iv. identifier

An identifier is the name of a syntax element, such as the namespace name, method name, and class name. In fact, all the items with names in the source code are named by identifiers. The rules for identifiers are as follows:

A) the first character must be a letter, underscore "_", or "@". The first character cannot be a number.

B) other characters must be the same as the first character (except @) and can also be numbers.

C) The length is less than or equal to 255 characters

D) The identifier cannot be a C # keyword.

5. Naming Conventions.

A) PascalCase, that is, the first letter of each word in the identifier uses uppercase letters, and the other letters Use lowercase letters.

B) we recommend that you use the "m _" prefix for the private instance Member name and the "S _" prefix for the private static member name, while the interface name starts with the "I" letter.

6. control structure;

A) the control structure of C # is almost identical to that of C ++. The switch statement has a slightly changed usage. In addition, a new loop statement foreach is added to traverse array elements.

B) Switch statement. Besides switch and case, there are also the break and default keywords.

BreakKeywords: When the break statement is executed, it will jump directly to the end of the switch statement. Note: if at least one statement exists in any branch of the switch statement, the statement must end with break, goto, or return.

DefaultKeywords:If the expression value cannot match any case statement, the code block after the default statement is executed. Note that the default statement block does not have to be placed at the end.

Class Program {

Static void Main (){

Int I = 6;

Switch (I ){

Case 1:

System. Console. WriteLine ("I equals 1 ");

Break;

Case 6:

System. Console. WriteLine ("I equals 6 ");

Break;

Default:

System. Console. WriteLine ("I is not equals to 1 or 6 ");

Break;

}

}

}

Type of the Switch statement:

  • Integer value types: sbyte, byte, short, ushort, int, uint, long, And ulong
  • Boolean bool
  • Enumeration type
  • String type string

C) loop statements (do, while, for, foreach)

BreakAnd ContinueStatement:

You can use continueThe statement skips the remaining part of the current loop to reach the next loop.

Available breakThe statement jumps to the end of the loop. This statement is also used in switchStatement to terminate the execution of a statement block. When a nested loop occurs, these two statement blocks apply to the last loop.

Class Program {

Static void Main (){

For (int I = 0; I <10; I ++ ){

System. Console. Write (I );

If (I = 2) continue ;//When I = 2, the remaining part of the current loop is skipped and the following statement is not executed.

System. Console. Write ("C ");

If (I = 3) break ;//When I = 3, it jumps to the end of the loop.

System. Console. Write ("B ");

}

}

}

The output result of the preceding statement is 0CB 1CB 2. 3C

D) The Break statement can also be used to terminate a so-called infinite loop. An infinite loop is always a for, do/while, or while loop when the condition is true.

While (true)

{}

 

E) Goto statement

Class Program {

Static void Main (){

Int I = 0;

Goto label2;

Label1:

I ++;

Goto label3;

Label2:

I --;

Goto label1;

Label3:

System. Console. WriteLine (I );

}

}

F) The Main () method is similar to C/C ++. The entry function of the C # executable program is called Main ();

1. But the main function in C #Must be staticYou can change the startup object settings on the Application tab of visual studio.

2. The Main () method must follow the following rules: the void or int type must be returned;OptionalString Array type. This string array contains the command line parameters of the application.

The first element represents the first parameter, and the second element represents the second parameter.

3. input parameters to the main function through project properties-debugging-command line parameters;

4. messages passed in from the command line (as well as environment variables) can be obtained through the string [] GetCommandLineArgs () and IDictionary GetEnvironmentVariables () Methods of the sytem. environment class.

Class Program {

Static void Main (String [] args ){

If (args. Length = 0)

System. Console. WriteLine ("You didn has different type any number to add .");

Else {

Long result = 0;

Foreach (string s in args)

Result + = System. Int64.Parse (s );

System. Console. WriteLine ("Sum: {0}", result );

}

}

}

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.