Manipulating managed data using unsafe

 While exploring the GC classes got a idea can we access a managed data using unmanaged code. So tried the following and got excited so here is the code

class Program
{
void Main(string[] args)
{
int a = 0;
unsafe
{
int *p = &a;
Console.WriteLine(“the value of a thru p={0}”,*p)
(*p)++;
}
Console.WriteLine(a);
}
}

Guess the output
The o/p is
the value of a thru p=0
1

It works fine with value type. I’m still struggiling to do the same for reference type. Looking for some hack…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: