C# supports pointers in a limited extent. A C# pointer is nothing but a variable that holds the memory address of another type. But in C# pointer can only be declared to hold the memory address of value types and arrays. Unlike reference types, pointer types are not tracked by the default garbage collection mechanism. For the same reason pointers are not allowed to point to a reference type or even to a structure type which contains a reference type. We can say that pointers can point to only unmanaged types which includes all basic data types, enum types, other pointer types and structs which contain only unmanaged types.
Declaring a Pointer type
The general form of declaring a pointer type is as shown below,
type *variable_name;
Where * is known as the de-reference operator. For example the following statement
int *x ;
Declares a pointer variable x, which can hold the address of an int type. The reference operator (&) can be used to get the memory address of a variable.
int x = 100;
The &x gives the memory address of the variable x, which we can assign to a pointer variable
int *ptr = & x;.
Console.WriteLine((int)ptr) // Displays the memory address
Console.WriteLine(*ptr) // Displays the value at the memory address.
Unsafe Codes
The C# statements can be executed either as in a safe or in an unsafe context. The statements marked as unsafe by using the keyword unsafe runs outside the control of Garbage Collector. Remember that in C# any code involving pointers requires an unsafe context.
We can use the unsafe keyword in two different ways. It can be used as a modifier to a method, property, and constructor etc. For example
// Author: [email protected]
using System;
class MyClass
{
public unsafe void Method()
{
int x = 10;
int y = 20;
int* ptr1 = &x;
int* ptr2 = &y;
Console.WriteLine((int)ptr1);
Console.WriteLine((int)ptr2);
Console.WriteLine(*ptr1);
Console.WriteLine(*ptr2);
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method();
}
}
The keyword unsafe can also be used to mark a group of statements as unsafe as shown below.
// Author: [email protected]
//unsafe blocks
using System;
class MyClass
{
public void Method()
{
unsafe
{
int x = 10;
int y = 20;
int* ptr1 = &x;
int* ptr2 = &y;
Console.WriteLine((int)ptr1);
Console.WriteLine((int)ptr2);
Console.WriteLine(*ptr1);
Console.WriteLine(*ptr2);
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method();
}
}
Pinning an Object
The C# garbage collector can move the objects in memory as it wishes during the garbage collection process. The C# provides a special keyword fixed to tell Garbage Collector not to move an object. That means this fixes in memory the location of the value types pointed to. This is what is known as pinning in C#.
The fixed statement is typically implemented by generating tables that describe to the Garbage Collector, which objects are to remain fixed in which regions of executable code. Thus as long as a Garbage Collector process doesn't actually occur during execution of fixed statements, there is very little cost associated with this. However when a Garbage Collector process does occur, fixed objects may cause fragmentation of the heap. Hence objects should be fixed only when absolutely necessary and only for the shortest amount of time.
Pointers & Methods
The points can be passed as an argument to a method as showing below. The methods can also return a pointer.
Ammar ShaukatPosted Oct 24, 2018, 8:49 AM
Very good organization of content . and good explanation as well.
Shubham JainPosted Nov 3, 2017, 2:55 PM
Can we use unsafe keyword in class defination?
Ramesh PalaniappanPosted Aug 29, 2016, 11:21 AM
Nice
Joe WilsonPosted Dec 30, 2015, 1:59 AM
Thank you for sharing.
Mahesh ChandPosted Dec 6, 2012, 5:33 AM
Noman, You can always add an "Article Extension" to add your version to an article. Cheers!
Noman AmeerPosted Nov 19, 2011, 6:11 AM
you have mention at beginning of your article that " A pointer is nothing but a variable that holds the memory address of another type" is wrong because pointer is a memory address not a variable.Pointer Variable is a variable that holds the address of variable. Please Correct it. Thanks. From : Noman Ameer Khan
eonsimiPosted Apr 13, 2011, 10:19 AM
I have one question: why do you declare a pointer like this: int *ptr? one thing is int and another int*! it`s confusing when reading because *ptr refers the value of the variable to which the pointer ptr points to. it is not the same thing to write: int *ptr = &x; and *ptr = &x; someone who will learn from your article will get confused.
RahatPosted Mar 14, 2011, 10:09 AM
i got this error Unsafe code may only appear if compiling with /unsafe