In this article, I am going to share information about Pointers in C#:
Pointers in C#
A pointer is simply a variable whose value is the address of another variable. We can not use pointers directly. To use a pointer, we need to have a specific block for it. The 'unsafe' keyword defines the scope to use pointer variables. Pointer variables can only be used inside an unsafe code block.
The syntax of a pointer variable is shown below.
datatype *<Variable_Name>;
For example,
- int* a;
- char* b;
- float* c;
Uses of pointers
- When interacting with unmanaged APIs or code
- When we have to make communication between two application using IPC
A method with unsafe keyword:
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static unsafe void Main(string[] args)
- {
-
- int p = 100;
-
-
- int* q = &p;
-
- Console.WriteLine("Value: " + p);
- Console.WriteLine("Address: " + (int)q);
- Console.ReadKey();
- }
- }
-
- }
Output
Value: 100
Address: 122743464
If we don't want to use the unsafe keyword on method definition, then there is another option through which we can use pointer variables inside a method. Have a look at the below example.
- using System;
-
- namespace Tutpoint
- {
- class TutLovers
- {
- public void Program()
- {
- int z = 50;
- Console.WriteLine("Before unsafe code block");
- unsafe
- {
-
- int p = 100;
-
-
- int* q = &p;
-
-
- int* r = &z;
- }
- Console.WriteLine("After unsafe code block");
-
-
-
- int* s = &z;
-
- }
- }
- }
Retrieving data from pointer variable:
There are some ways through which we can retrieve the value from a pointer variable, as shown below.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static unsafe void Main(string[] args)
- {
-
- int p = 100;
-
-
- int* q = &p;
-
- Console.WriteLine("Value: " + p);
-
-
- Console.WriteLine("Value: " + *q);
-
- Console.WriteLine("Value: " + q->ToString());
- Console.WriteLine("Value: " + (int)q);
- Console.ReadKey();
- }
- }
-
- }
Output
Value: 100
Value: 100
Value: 100
Value: 113044312
Pointer variable as Method's parameter
Pointer variables can be used as parameters of a method, as shown below,
- using System;
-
- namespace Tutpoint
- {
- unsafe class Program
- {
- unsafe int* ss;
- static unsafe void Main(string[] args)
- {
- int value1 = 100;
- int value2 = 500;
- int* q = &value1;
- int* r = &value2;
-
- Program.sample(q, r);
- Console.ReadKey();
- }
-
- public static unsafe void sample(int* a, int* b)
- {
- int x = *a;
- int y = *b;
- Console.WriteLine("X Value: " + x);
- Console.WriteLine("Y Value: " + y);
-
- }
-
- }
-
- }
Output
X Value: 100
Y Value: 500
Note
- In order to compile the code, we have to check the "allow unsafe code" on Project-> Right Click-> Properties-> Build tab otherwise, an error will produce as "Unsafe code may only appear if compiling with /unsafe".
- void* <variable> can also be used but usually, it is not recommended to use. It is basically a pointer of unknown type
- Use of unsafe code enhances security risks.
Conclusion
Pointers should only be used when it is really necessary because we are going out of security on risk. Also, we have to make a compromise with the automatic garbage collector.