1).Unsafe clause could define a method, property or ctor(unstatic ctor) as unsafe.The unsafe method is from the parameters of method to the end of the method.
for example:
1static unsafe void FastCopy(byte* ps, bype* pd,int count)
2{
3 //unsafe environment;
4}
2{
3 //unsafe environment;
4}
unsafe
{
unsafe clause1;
unsafe clause2;
}
{
unsafe clause1;
unsafe clause2;
}
an complete small test code like this:
using System;
class UnsafeTest
{
//here we could find "unsafe static"=="static unsafe",compile result is the same.
unsafe static void SquarePtrParam(int* p)
{
*p*=*p;
}
[STAThread]
public static void Main(string[] args)
{
int i=5;
unsafe
{
SquarePtrParam(&i);
}
Console.WriteLine("i"+i);
}
}
class UnsafeTest
{
//here we could find "unsafe static"=="static unsafe",compile result is the same.
unsafe static void SquarePtrParam(int* p)
{
*p*=*p;
}
[STAThread]
public static void Main(string[] args)
{
int i=5;
unsafe
{
SquarePtrParam(&i);
}
Console.WriteLine("i"+i);
}
}
Plus: if you want to compile unsafe code under the CLR, you need to mention /unsafe to compiler directly. You can set this property in your project property page (right click the project in solution manager explore, then you will find it at the bottom of the menu.)Then, click the configuration property ,and choose "generation"-- the first subchoice.
You will find the default property of allowing unsafe check is false .You need to set it to "True".
For further study ,I want to explore the difference between the safe method and the unsafe.That 's need knowledge of IL language. I hope I could explore after getting a book about IL.:)
Note: For my .NET IDE is a Chinese version, I don't know whether words about the english menu are correct. Sorry~~
No comments:
Post a Comment