Friday, August 20, 2010

Unsafe Code ( Using Pointer) in C#

Unsafe Code ( Using Pointer in C#)

C#.NET hides most of memory management, but in some cases we need direct access to the memory, then unsafe code is used.


Eg:
using System;
class Mainclass
{
public static void Main()
{
int a = 5;

unsafe
{
int* ptr;
ptr = &a;
*ptr = 20;
}
Console.WriteLine("a = " + a);
}
}



Eg:

using System;
unsafe class Mainclass
{
public static void Main()
{
int a = 5;
int* ptr;
ptr = &a;
*ptr = 20;


Console.WriteLine("a = " + a);
}
}

No comments: