C # unsafe code
C # allows pointer variables to be used in a function when a block of code uses the unsafe modifier tag. Unsafe or unmanaged code refers to a block of code that uses a pointer variable.
Pointer variable
A pointer is a variable that has the address of another variable, that is, the direct address of the memory location. Just like any other variable or constant, you must declare the pointer before you use the pointer to store other variable addresses.
The general form of a pointer variable declaration is:
Type *var-name;
The following is a valid pointer declaration:
int *ip; /* point to an integer */double *DP; /* point to a double-precision number */float *FP; /* point to a floating-point number */char *ch/ * point to a character */
The following example illustrates the use of a pointer when the unsafe modifier is used in C #:
Using System;namespace unsafecodeapplication{ class program { static unsafe void Main (string[] args) { int var =; int* p = &var; Console.WriteLine ("Data is: {0}", var); Console.WriteLine ("Address is: {0}", (int) p); Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
Data is:20address is:99215364
You can also not declare the entire method as unsafe code, only part of the method is declared as unsafe code. This is illustrated in the following example.
Retrieving data values using pointers
You can use the ToString () method to retrieve data stored in the position referenced by the pointer variable. The following example demonstrates this:
Using System;namespace unsafecodeapplication{ class program {public static void Main () { unsafe { int var =; int* p = &var; Console.WriteLine ("Data is: {0}", var); Console.WriteLine ("Data is: {0}", p->tostring ()); Console.WriteLine ("Address is: {0}", (int) p); } Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
Data Is:20data is:20address is:77128984
Passing pointers as arguments to methods
You can pass a pointer variable to a method as a parameter to a method. This is illustrated in the following example:
Using System;namespace unsafecodeapplication{ class Testpointer {public unsafe void Swap (int* p, int *q) c3/>{ int temp = *p; *p = *q; *q = temp; } Public unsafe static void Main () { Testpointer p = new Testpointer (); int var1 = ten; int var2 =; int* x = &var1; Int* y = &var2; Console.WriteLine ("Before Swap:var1:{0}, Var2: {1}", var1, var2); P.swap (x, y); Console.WriteLine ("After Swap:var1:{0}, Var2: {1}", var1, var2); Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
Before Swap:var1:10, Var2:20after swap:var1:20, Var2:10
Using pointers to access array elements
In C #, the array name and a pointer to the same data type as the array data are different variable types. For example, int *p and int[] p are different types. You can increase the pointer variable p because it is not fixed in memory, but the array address is fixed in memory, so you cannot increase the array p.
Therefore, if you need to use pointer variables to access the array data, you can use the fixed keyword to pin the pointer as we usually do in C or C + +.
The following example demonstrates this:
Using System;namespace unsafecodeapplication{ class Testpointer {public unsafe static void Main () { int[] list = {Ten, +, +}; Fixed (int *ptr = list)/ * Displays the array address in the pointer * /for (int i = 0; i < 3; i++) { Console.WriteLine ("Address of Li St[{0}]={1} ", I, (int) (PTR + i)); Console.WriteLine ("Value of List[{0}]={1}", I, * (ptr + i)); } Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
Address of list[0] = 31627168Value of list[0] = 10Address of list[1] = 31627172Value of list[1] = 100Address of list[2] = 31627176Value of list[2] = 200
Compiling unsafe code
In order to compile unsafe code, you must switch to the command-line compiler to specify the/unsafe command line.
For example, to compile a program named Prog1.cs that contains unsafe code, you would enter a command on the command line:
Csc/unsafe Prog1.cs
If you are using the Visual Studio IDE, you need to enable unsafe code in the project properties.
The steps are as follows:
Open Project properties by double-clicking the Properties node in Resource Manager (Solution Explorer).
Click the Build tab.
Select the option "Allow unsafe code".
The above is the "C # tutorial" C # Unsafe code content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!