When I was watching the C # video, I felt that this teacher was very confused. Fortunately, VB was used as the basis. As long as you still remember the summary of VB discussed last year, you will feel very familiar when watching the C # Video. After watching the video, you have a special desire to compare C # With VB.
1. Data Type
C # includes two types of variables: Value Type and reference type.
1. Value Type: The value type is a volume containing actual data. When defining a value type variable, C # allocates a storage area suitable for the size of the variable in stack mode based on the declared type, then read and write the variable.
For example, in the following example, a 32-bit storage area is allocated to variable A in the stack, and 10 is placed in the memory area. Then, the value is retrieved from variable, add 10 and then assign the calculation result to.
Int a = 10 A = a +10
2. Reference Type: The reference type is different from the value type. The value type stores the actual amount of data, while the reference type stores the reference of the memory address of the data.
The following two steps are generally involved in creating a reference type: first, create a reference variable on the stack, then create the object on the stack, and then assign the first address of the memory to the reference variable.
For example:
String S1,S2;S1="ABCD";S2 = S1;
Here, string is a reference type, and S1 and S2 are reference variables pointing to strings respectively. S1 is an address of the string "ABCD" stored in the memory. Assign values between two referenced variables (S1 and S3) so that both of them are references to "ABCD.
Note: The difference between heap and stack:
Heap is the memory allocated by new. It stores variable-length data and allocates and releases memory in any order and size. It is slow but easy to use. Stack is generally used to store data of a fixed length. Store data items according to the advanced and later principles.
Compared with the C # data type, the VB data type is much simpler, mainly including the numeric type, currency type, byte type, date type, logic type, string type and object type.
2. Variables and constants
1. constants are unchangeable quantities.
The constant types defined in C # Are intdouble string Boole char.
Syntax: const type name constant name = constant expression
Const double Pi = 3.14159
Constant initialization: const int A = 1
Constants in VB mainly include integer, long and byte constants, real constants, string constants, logical constants, and datetime constants. The statement is as follows:
[Public | private] const <constant Name> [as] <type>] =
<Expression>
2. Variable refers to the amount that can be changed while the program is running.
Variable declaration in C:
[Access restriction keyword] data type variable name
PS:
The variable name must start with a letter or underscore and cannot contain special characters;
Multiple variables of the same type can be declared in a variable declaration.
Variable scope:
Private is accessible only by yourself.
Internal can be accessed in the same assembly
Public
Protected can be accessed only by the inheritance relationship
Static variables and instance variables: static variables belong to the class, and instance variables belong to the class instance.
Variables in VB:
Declaration: <public | private | dim | static> <variable name> [as data type]
Scope of variables in VB:
Local variable: dim or static
Module-level variables: private or dim
Global variable: Public
Operators and expressions:
C # provides a large number of operators: the operators are divided by the number of operands. There are unary operators (++/--), binary operators (+, *), and ternary operators (? :) There are several categories based on the computing functions:
Arithmetic Operators, Relational operators, logical operators, value assignment operators, conditional operators, bitwise operators, and other special operators.
In VB, operators are much simpler, mainly including Arithmetic Operators, string operators, Relational operators, and logical operators.