Clear, practical technology insights About · Contact

Cursor in C #: Safe Code in C #

Understand Cursor in C #: Safe Code in C # with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts...

Published: 7 minutes read
Table of Contents

Cursor in C #: Safe Code in C # is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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:

Pointer in C #

A pointer is a variable whose value is the address of another variable, For instance,, 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 example below 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:

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:

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:

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 instance,, 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 (); } } } 

Frequently Asked Questions

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:

What should you know about pointer in C #?

A pointer is a variable whose value is the address of another variable, For instance,, 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.

What should you know about 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:

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.