Search
×

Sign up

Use your Facebook account for quick registration

OR

Create a Shvoong account from scratch

Already a Member? Sign In!
×

Sign In

Sign in using your Facebook account

OR

Not a Member? Sign up!
×

Sign up

Use your Facebook account for quick registration

OR

Sign In

Sign in using your Facebook account

Unsafe Keyword

Article Review   by:Aaathar    
ª
 
The unsafe keyword denotes an unsafe context. A unsafe context is required for any operations that you want to perfrom with pointers. unsafe is applied as a modifier in the declaration of callable member such as methods, properties, constructors and extenders (although static constructors are not permitted).

static unsafe void ComplexOp()
{
//unsafe context with use of pointers here
}

The scope of the unsafe includes the parameter list to the end of the function, thus allowing pointers in the parameter list such as:

static unsafe void ComplexOp(byte* ps, int count)
{
//unsafe context with use of pointers here
}

Note: When compiling unmanaged code you have to specify the /unsafe compiler switch.

Example:

// unsafe keyword
using System;

class UnSafe
{
// unsafe method that takes a pointer to integer (int)
unsafe static void AddPtr (int* p)
{
*p += *p;
}

unsafe public static void Main()
{
int iApples = 10;
AddPtr(&iApples);
Console.WriteLine(iApples);

// pauses output window
Console.ReadLine();
}

}
Published: August 24, 2010   
Please Rate this Review : 1 2 3 4 5
Translate Send Link Print
X

.