C # BASIC Language Understanding

Source: Internet
Author: User

I have been learning C # for a few days. After reading the language basics, I made a summary of C # language basics to consolidate the recorded knowledge and hope to help you.

I have been watching the C # Video recently, but I didn't understand it at the beginning. Later I asked the master to discuss it. I felt like I was watching it again, so I started again. Summarize the C # language basics of these two days.

Data Type

Composition: data type, constants and variables, operators and expressions, arrays, structures, and enumerations

Data types include value type, reference type, packing, and unpacking.

Value Type data is stored in the stack. STACK: it is used to store data of a fixed length. For example, INT (each int occupies four bytes). Each program has its own stack during execution, and other programs cannot access the stack.

Reference data is stored in the heap. Heap: Memory allocated by new. It is generally slow and prone to memory fragments. However, it is most convenient to use. (Although there are not many words to explain the heap, I always feel that the heap is a little abstract and doesn't quite understand it .)

Value Type

The value type is a volume that contains actual data. When we define a value type variable, C # allocates a corresponding storage area to the variable in stack mode based on the declared type. (The stack can only be understood here, but it cannot be expressed. I hope you can give some advice .)

Value types include simple type, enumeration type, and structure type.

Simple Type

Some features of simple type sharing in C. First, they are all aliases of the. NET system type. Second, constant expressions composed of simple types are only detected during compilation rather than runtime. Finally, simple types can be initialized literally. C # simple type classification:

The simple type is preset by the system, which can be integer, floating point, decimal, or Boolean. In the first year of my advanced training, I had some basic information about VB. Here I feel that I can compare it with VB.

We can see through comparison.

1. The meanings (ranges) of integer, long, byte, and C # In VB are the same.

2. In VB, the single precision and double precision are consistent with the meaning (range) expressed by the C # floating point type.

3. The currency type of VB is similar to that of C # decimal type. (However, it is obvious that the currency range indicated by C # is far beyond the range indicated by the VB currency type)

4. Both VB and C # Have the string type and boolean type.

5. What is obviously different is the special date type of VB.

With the basic understanding of VB, there is no difficulty in C. What is lacking is their proficiency.

 

Structure Type

The process of organizing a series of related information into a single entity is the process of creating a structure.

Struct person {string m_name; // name int m_age; // age string m_sex; // gender}

 

Enumeration type

It is mainly used to represent a logically associated item and combination. Use the keyword Enum to define.

    enum Weekday    {      Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday    }

 

Reference Type

Variables of the reference type do not store the actual data they represent, but store the reference of the actual data. The reference type is created in two steps: create a reference variable on the stack, create the object itself on the stack, and assign the memory handle (the first address of the memory) to the reference variable.

Example:

     String S1,S2;     S1=”ABCD”;     S2=S1;

 

 

Explanation: both S1 and S2 point to the referenced variable of the string. The S1 value is the string "ABCD" stored in the memory address. This is the reference to the string, the assignment between two referenced variables makes S1 and S2 both reference "ABCD.

Reference types include class, interface, array, Delegate, object, and string.

Unpacking and packing

The conversion between the value type and the reference type is called packing and unpacking. Packing and unpacking are the core of the C # type system. We can easily convert the value type and reference type by packing and unpacking.

 

For example:

Int num = 123; // assign the 123 value to the int type variable num object P = num; // binning int q = int (p) // binning

 

Constants and variables

A constant is the amount that the value does not change during the running of the program.

Variable is the amount of variable values that can be changed during the program running.

Operators and expressions

Arithmetic Operator:

Relational operators:

Logical operators (&&) Or (|) Non (!)):

Assignment operator:

Conditional operators:

A conditional operator is a ternary operator and can be considered as an if ...... Else simplified form.

Format: condition? True: false

Example:

Enter the following code in the console:

           int x = 10;           int y = 15;           int z;           Console.WriteLine (z = x > y ? x : y);

 

Obviously, y indicates false.

Process Control Condition Statement

If statement

Sentence:

First:

If (condition) statement;

 

Second:

If (condition) {statement block} else {statement block}

 

In addition, the IF statement can be nested in the IF statement.

Switch statement

A switch is a multi-branch statement. Syntax structure:

 

Switch (expression) {Case constant 1: Statement sequence 1; break; Case constant 2: Statement sequence 2; break ;...... Default: Statement sequence N; break ;}

 

Loop statement

While statement

While (expression) {loop body statement block}

Do while statement

Do {loop body statement block} while (expression );

For
Statement

For (expression 1; expression 2; expression 3) {loop body statement}

 

Expression 1 is the initial value of the loop control variable, expression 2 is a Boolean expression, and expression 3 is the value-added value of the loop control variable (both positive and negative values are acceptable ). PS: The three expressions are optional. When the default expression is used, ";" cannot be saved.

Foreach statement

The foreach statement is newly introduced by C #. It indicates that each element in a set is collected and embedded statements are executed for each element.

Syntax format:

Foreach (in set expression) statement;

 

The identifier refers to the iteration variable of the foreach loop. It is valid only in the foreach statement and is a read-only local variable. That is to say, this iteration variable cannot be rewritten in the foreach statement.

A set expression is a traversal set, such as an array.

Example:

Int m = 0; string mystring = "fsofknwlsnfoaafdagadf"; foreach (char mychar in mystring) {If (mychar = 'A') m ++;} console. writeline ("string contains {0} A", M );

Result

Jump statement

The jump statement is used to change the execution process of the program and transfer the details. Used in a specific context.

Continue statement

The continue statement can only be used in loop statements. It ends the current loop and does not execute the remaining loop body statements. For the while and do_while structures, after the continue statement is executed, immediately test the loop condition to determine whether the loop continues. For the for structure loop, after the continue is executed, evaluate expression 3 (the increment part of the loop) first, and then test the loop condition.

For example, an integer that can be divisible by 3 in 1-is displayed.

           for (int n = 1; n <= 100; n++)           {                if (n % 3 != 0)                    continue;                {                    Console.WriteLine(n );                }           }

 

Result:

 

Break statement

The break statement can only be used in a loop statement or switch statement. If the break statement is executed in a loop statement, the loop ends immediately and jumps to the next statement of the loop statement. No matter how many layers of the loop, the break statement can only jump out of one layer from the innermost layer that contains it. If you execute the break statement in the switch statement, immediately jump out of the switch statement and go to the next statement of the switch statement.

See the following example:

Return Statement

The return statement appears in a method. When the return statement is executed in the method, the execution process in the program is transferred to the method called. If the method has no return value (return type modifier void), return in the "return" format. If the method has a return value, use the "Return expression" format, the following expression is the return value of the method.

GOTO statement

The GOTO statement is flexible in transferring the execution process of a program from one place to another. However, because of its flexibility, it is easy to cause program structure disorder and the GOTO statement should be used in a controlled and reasonable manner.

Exception Handling

Program exception handling can make the program more robust. I will not describe more here.

Array, structure, and enumeration

Array: An array is a data structure that contains several variables. These variables have the same data type and are sorted, therefore, you can use a uniform array name and subscript to uniquely identify the elements in the array.

When using an array, you must first declare it and then initialize the array at the end of the creation. The syntax format is as follows:

Type [ ] arrayname;Arrayname =new type[size] {  val1,val2,val3,….valn  };

 

Or write them together:

Type [ ] arrayname = new type [size ] {  val1,val2,val3,….valn  };

Type can be any data type in C.

[] Indicates that the following variable is an array type and must be placed before the array name.

Arrayname
Is an array name, which complies with the naming rules of identifiers.

Size indicates the number of primary colors in the array.

Val indicates the specific value of the array.

Example:

Example 1:

Int [] course = new int [10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} // student is an array containing 10 int types.

 

Example 2: Create an array of 5 numbers (, 50, 60, 70, 99) and calculate their maximum and minimum values.

The Code is as follows:

# Region array instance int Max; int min; int [] num = new int [5] {100, 50, 60, 70, 99 }; max = min = num [0]; for (INT I = 0; I <= 4; I ++) {If (Num [I]> MAX) {max = num [I];} If (Num [I] <min) {min = num [I] ;}} console. writeline ("the maximum number of the required array is" + MAX + "; the minimum number is" + min); # endregion

 

The structure and enumeration and value types are described in this document.

In the process of summing up the basics of C # language, I also constantly look at the book Visual Basic I learned before. I have to say that it is much easier to learn VB in C. There are many similarities between them. During the comparison with VB, I also learned a lot of things I didn't understand before.

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.