In fact, the whole. Net project is hosted code running on the. NET Framework. This principle is similar to that of Java virtual machine.
To put it simply, managed code cannot directly write memory, which is safe, rather than non-secure code. You can use pointers to operate on memory.
Generally, you can use managed code for projects, that is, you do not need to use non-secure code in the program.
For some features that require high speed, you can consider using non-secure code, using pointers and other read/write memory. For a real project, it is still managed security code.
For more information about Unsafe code, see msdn unsafe.
In the string method, string operation methods such as toupper generate a new string, which increases the running expense. An alternative is to operate strings directly using unmanaged code. For example, replace toupper:
Using system;
Public class test
{
Public static void main (string [] ARGs)
{
String STR = 'hello ';
Toupper (STR );
Console. writeline (STR );
}
Private Static unsafe void toupper (string Str)
{
Fixed (char * pfixed = Str)
For (char * P = pfixed; * P! = 0; P ++)
{
* P = Char. toupper (* P );
}
}
}
Fixed statement:
Format: fixed (type * PTR = expr) Statement
It aims to prevent variables from being located by the garbage collector.
Where:
Type is not managed type or void
PTR is the pointer name.
Expr is an expression that can be implicitly converted to type *.
Statement is an executable statement or block.
The fixed statement can only be used in the context of unsafe. The fixed statement sets a pointer to the managed variable and "locks" the variable during statement execution. If there is no fixed statement, the pointer to the managed variable will have little effect, because garbage collection may be unpredictable to relocate the variable.
After statement is executed, all locked variables are unlocked and restricted by garbage collection. Therefore, do not point to variables other than fixed statements. In unsafe mode, memory can be allocated on the stack. The stack is not restricted by garbage collection, so it does not need to be locked.
However, during compilation, the use of unmanaged code requires/unsafe to pass.
In simple terms, managed code is to manage the memory (memory application, memory release, garbage collection, and so on)
All are. net CLR management, that is to say, the use of managed code to encapsulate some underlying operations, cannot directly READ memory and other hardware-related operations, the advantage is to compare
Secure, there will be no problems such as memory leakage, and the disadvantages are also obvious. The memory cannot be directly read, and performance will be compromised. Sometimes it is not flexible enough to use.
Instead of hosting, you can directly perform hardware operations with high performance, but with high requirements for developers.
The most intuitive thing is that C # does not recommend using pointers, while C ++ can use pointers to directly READ memory;
C # Use garbage collection to manually release objects in C ++ ......