-------------------
Boxing and unpacking operations (boxing / unboxing)
-------------------
C # packing ideas are brand new. The above mentioned all data types, whether it is built-in or user-defined, all from a base class Object from the namespace SYSTEM. Therefore, the basic or original type is converted to the Object type is called the box, in turn, the inverse operation of this method is called a split.
Example:
Class test
{
Static void
Main
()
{
INT myint = 12;
Object obj = myint; / / Packing
INT myint2 = (int) OBJ; // Unbo
}
}
The example shows the boxes and unpacking operations. A integer value is converted into an Object type and then converts back to the integer. When a value type variable needs to be converted into a reference type, an Object's box will be assigned a space that houses this value, which will be copied into this box. In contrast, the data in an Object box is converted into its original value type, this value will be copied from the box to the appropriate storage location.
-------------------
Method parameters
-------------------
There are three types of parameters in C #:
Value parameter / input parameters
Reference type parameter / input output type parameters
OUT parameters
If you have a COM interface and its parameter type concept, you will easily understand the C # parameter type.
Value parameter / input parameters
The value concept is the same as C . The value to be transmitted will be copied to a location and passed to the function.
Example:
Setday (5);
...
Void setday (int day)
{
....
}
Reference type parameter / input output parameters
The reference parameters in C # are neither C pointers nor the reference operator (&) to pass. The reference type parameter of C # reduces the possibility of an error. The reference type parameters are also referred to as input and output parameters, because you pass a reference address, so you can pass an input value from a function and you can get an output value.
You cannot pass an uninitial reference parameter to a function. C # with Ref this keyword to declare reference type parameters. When you pass a variable to the reference parameters required for the function, you must use a REF keyword description.
Example:
INT A = 5;
FunctionA (Ref a); // To declare the variable with the REF, you will get
// A compilation error
Console.writeLine (a); // The value of the point to the address is 20
Void Functiona (Ref Int Val)
{
INT x = VAL;
VAL = x * 4;
}
OUT parameters
The OUT type parameter only returns a value from the function. No input values are required. C # with keyword OUT to describe this parameter
Example:
Int Val;
GetNodeValue (VAL);
Bool GetNodeValue (Out Int Val)
{
Val = value;
Return True;
}
Variable number of parameters and arrays
The array is transmitted by keyword params in C #. As the variable of the array type, you can pass any number of elements. From the example below, you can understand better.
Example:
Void Func (params int [] array)
{
Console.writeline ("Number of Elements {0}", array.length);
}
Func (); //prints 0
Func (5); // Prints 1
FUNC (7, 9); // Prints 2Func (new int → {3, 8, 10}); // Prints 3
Int [] array = new int [8] {1, 3, 4, 5, 5, 6, 7, 5};
Func (array); //prints 8
-------------------
Operators and expressions
-------------------
Operators and expression concepts are identical to C . But some new useful operators are filled in. I will discuss some of these parts here.
IS operator
The IS operator is used to check whether the type of operand is the same or whether it can be converted. The IS operator is particularly useful in a polymorphic environment. It has two operands, and the calculation results are a Boolean. Look at this example:
Void Function (Object Param)
{
PARAM IS ClassA
// do something
Else IF (param is mystruct)
// do something
}
}
AS operator
If the AS operator checks if the type of operand can be converted or whether these AS is completed by the IS operator. If the result is convertible, the result will be converted or boxed to be Object (About the AS operator Please see the previous packing / unpacking operations in the box). If you do not convert or pack, the return value is null. Hey, the following example we will better understand this concept.
Shape SHP = New Shape ();
Vehicle veh = SHP as vehicle; / / The result is NULL, the type is not convertible
Circle Cir = new circle ();
Shape SHP = CIR;
Circle Cir2 = SHP as circle; // will be converted
Object [] Objects = new object [2];
Objects [0] = "AISHA";
Object [1] = new shape ();
String Str;
For (int i = 0; I & { Str = Objects [i] AS String; IF (str == NULL) Console.writeline ("Can Not Be Converted"); Else Console.writeLine ("{0}", STR); } Output: Aisha Can Not Be Converted ------------------- Statement ------------------- In addition to some new statements and modifications to certain statements, C # statements and C are very similar. Here is a new statement: Foreach Used to access set elements sequentially, such as image arrays, etc. Example: Foreach (String S in Array) Console.WriteLine (s); LOCK Used to lock the code block so that the thread is in a critical dispatch zone, and the other threads cannot enter the locking critical area. Checked / unchecked Overflow detection in numerical calculations. Example: INT x = int32.maxvalue; x ; // overflow detection { X ; // abnormal } unchecked { X ; // overflow} } The following statement has been modified in C #: Switch After executing a CASE statement, the program process does not allow jump to the next neighboring CASE statement. This is permissible in C . Example: INT var = 100; Switch (var) { Case 100: console.writeline (" / / No Break statement Case 200: console.writeline (" } C compiled output: C # under, compile time error: ERROR CS0163: Control Cannot Fall Through From One Case Label ('Case 100:') To Another But you can still do C similar things Switch (var) { Case 100: Case 200: Console.WriteLine ("100 or 200 Break; } You can also constant variables as the value of Case: Example: Const string weekend = "sunday"; Const string weekday1 = "monday"; .... String weekday = console.readline (); Switch (weekday) { Case Weekend: Console.writeline ("It's weekend !!"); Case Weekday1: console.writeline ("it's monday"); Break; }