What is the use of unsafe keyword in C#?

 Posted by Abhisek on 12/25/2009 | Category: C# Interview questions | Views: 8425
Answer:

In C# the value can be directly referenced to a variable, so there is no need of pointer. Use of pointer sometime crashes the application. But C# supports pointer, that means we can use pointer in C#.

The use of pointer in C# is defined as a unsafe code. So if we want to use pointer in C# the pointer must be present in the body of unsafe declaration. But pointer does not come under garbage collection.

Example:-

unsafe
{
int a, *b;
a = 25;
b = &a;
Console.WriteLine("b= {0}",b);//returns b= 25
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response

More Interview Questions by Abhisek