Pointers In C#


Here is a simple example of how to use pointers using C#. It is short and easy to follow.

//Compilation
//CSC FileName.CS

using System;

class Test
{
unsafe static void WriteLocations()
{
int *p;
int i;
i = 10;
p = &i;

string addr;
addr = int.Format((int) p, "X");
//To Format the Pointer to a String

Console.WriteLine(addr);
Console.WriteLine(*p);

*p = 333;
Console.WriteLine(i);
//Change the Value using Pointer

i = *(&i) + 10;
Console.WriteLine(i);

}

static void Main()
{
WriteLocations();
}
}

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!