Pointer in .NET

Question posted in Computer Software on 06 2010
Rate question difficulty level 1 Votes
Is it possible to create a pointer in .NET ?
If yes, please write how ?
If no , what id the reason ?
 
 
3 Answers
 
Yes, we can create pointers. These are known as unsafe code.

We can apply these as :

unsafe
{
int* pi;
int intx = 1;
pi = &x;
System.Console.WriteLine("Value of intx is: " + *pi);
}

06/30/2010
 
 
Yes it is in unsafe mode. You must set compiler option that you will work in unsafe mode. Then every method that will use pointers in it, it's signature must be declared with unsafe.
eg. public unsafe void MyPointerIterator(){

// Inside comes the pointer declaration
// eg. int i = 0;
// int* counter = &i;

}

06/30/2010
 
 
Yes, pointers can be used in .net, but there is a limitation on its usage.
1. Pointers can be used in a code block/snippet which is marked as 'unsafe' - unsafe can be either modifier or a method
2. Pointers can be used for only value types and arrays, because GC can not have hold over pointers.

Usage -

public unsafe void DemoPointer()
{
int x = 10;
int *iPtr = &x;
Console.WriteLine((int)iPtr);
Console.WriteLine(*iPtr);
}

07/01/2010
 
 
Add an answer*
 
Your name
Email
 
Company: iforex
Location: Israel
.NET CLR C# VB.NET

add a question

arrow_blue


Now hiring!