Safe code in C #

C # allows using pointer variables in a function of code block when it is marked by unsafe modifier. The concept of unsafe code or unmanaged code in C # is a code block that uses a pointer variable.

C # allows using pointer variables in a function of code block when it is marked by unsafe modifier. The concept of unsafe code or unmanaged code in C # is a code block that uses a pointer variable.

How to compile unsafe code in Visual Studio 2010

To compile and run programs in / unsafe mode, just right-click on Project , then select Properties -> Build -> Allow unsafe code and then press Ctrl + S to save the changes as shown in the picture:

Picture 1 of Safe code in C #

Pointer in C #

A pointer is a variable whose value is the address of another variable, for example, the direct address of the memory location. Similar to any other variable or constant in C #, you must declare a pointer before you can use it to store any variable address.

The general form of a cursor declaration in C # is:

 type * var - name ; 

Here are valid cursor declarations in C #:

 int * ip ; /* con trỏ tới một số nguyên */ double * dp ; /* con trỏ tới một số double */ float * fp ; /* con trỏ tới một số float */ char * ch /* con trỏ tới một ký tự */ 

The following example illustrates the use of pointers, using unsafe modifier in C #:

 using System ; namespace QTMCsharp { class TestCsharp { static unsafe void Main ( string [] args ) { Console . WriteLine ( "Con tro trong C#" ); Console . WriteLine ( "--------------------------------" ); int var = 20 ; int * p = & var ; Console . WriteLine ( "Du lieu: {0} " , var ); Console . WriteLine ( "Dia chi: {0}" , ( int ) p ); Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

Picture 2 of Safe code in C #

Instead of declaring the entire method in unsafe format, you can also declare an unsafe code.

Revoke data values ​​by using pointers in C #

You can retrieve the data stored at the location referenced by the pointer variable, using the ToString () method in C #. Here is an example:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Con tro trong C#" ); Console . WriteLine ( "Unsafe code trong C#" ); Console . WriteLine ( "--------------------------------" ); unsafe { int var = 20 ; int * p = & var ; Console . WriteLine ( "Du lieu: {0} " , var ); Console . WriteLine ( "Du lieu: {0} " , p -> ToString ()); Console . WriteLine ( "Dia chi: {0} " , ( int ) p ); } Console . ReadKey (); } } } 

Compiling and running the above C # program will produce the following results:

Picture 3 of Safe code in C #

Pass the Cursor as Parameters to the method in C #

You can pass a pointer variable to a method as parameters. Here is an example:

 using System ; namespace QTMCsharp { class TestCsharp { public unsafe void swap ( int * p , int * q ) { int temp = * p ; * p = * q ; * q = temp ; } public unsafe static void Main () { Console . WriteLine ( "Unsafe code trong C#" ); Console . WriteLine ( "Truyen con tro nhu la tham so" ); Console . WriteLine ( "-----------------------------------" ); TestCsharp p = new TestCsharp (); int var1 = 10 ; int var2 = 20 ; int * x = & var1 ; int * y = & var2 ; Console . WriteLine ( "Truoc khi trao doi: var1 = {0}, var2 = {1}" , var1 , var2 ); p . swap ( x , y ); Console . WriteLine ( "Sau khi trao doi: var1 = {0}, var2 = {1}" , var1 , var2 ); Console . ReadKey (); } } } 

Compiling and running the above C # program will produce the following results:

Picture 4 of Safe code in C #

Access array elements using a pointer in C #

In C #, an array name and a pointer to the same data type as array data, are not the same type of variable. For example, int * p and int [] p, are not of the same type. You can quantify pointer p because it is not fixed in memory, but an array address is fixed in memory, and you cannot quantify it.

Therefore, if you need to access an array data using a pointer variable, as we did in C or C ++ (you can check: Cursor in C ), you need to fix that pointer Use the keyword fixed in C #.

Here is an example to illustrate:

 using System ; namespace QTMCsharp { class TestCsharp { public unsafe static void Main () { Console . WriteLine ( "Unsafe code trong C#" ); Console . WriteLine ( "Truy cap cac phan tu mang boi su dung con tro" ); Console . WriteLine ( "-----------------------------------" ); int [] list = { 10 , 100 , 200 }; fixed ( int * ptr = list ) /* mang dia chi trong con tro */ for ( int i = 0 ; i < 3 ; i ++) { Console . WriteLine ( "Dia chi cua list[{0}]={1}" , i , ( int )( ptr + i )); Console . WriteLine ( "Gia tri cua list[{0}]={1}" , i , *( ptr + i )); } Console . ReadKey (); } } } 

Compiling and running the above C # program will produce the following results:

Picture 5 of Safe code in C #

Follow tutorialspoint

Previous article: Anonymous method in C #

Next article: Multithread (Multithread) in C #

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile