1. Code that runs in the CLR virtual machine becomes a regulated code
Actually the whole. NET projects are managed code that runs on the. NET Framework, which is similar to the mechanism of a Java virtual machine.
In the simplest terms, managed code cannot write directly to memory, is safe, and unmanaged code is non-secure, and you can manipulate memory using pointers .
Generic projects use managed code, which means that non-secure code is not needed in the program.
For some features that require high speed, consider using non-secure code, reading and writing memory using pointers, and managed security code for a project.
For information about non-secure code, see MSDN unsafe.
The
In the string method, such as ToUpper, generates a new string, which increases the cost of running the operation. An alternative is to manipulate strings directly through unmanaged code. As an alternative to the ToUpper method:
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=PFI xed;*p!=0;p++)
{
*p = char. ToUpper (*P);
}
}
}
Fixed statement:
format fixed (type* ptr = expr) Statement
It is designed to prevent variables from being located by the garbage collector.
Where:
type is an unmanaged type or void
ptr is a pointer name
expr is an expression that can be implicitly converted to type*
statement is an executable statement or block
Fixed statement can only be made 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, pointers to managed variables will work very small because garbage collection may relocate variables unpredictably. When the
finishes executing the statement, any locked variables are unlocked and subject to garbage collection. Therefore, do not point to those variables other than the fixed statement. In unsafe mode, memory can be allocated on the stack. The stack is not subject to garbage collection, so it does not need to be locked.
But at compile time, because unmanaged code is used, you must use/unsafe to pass.
To put it simply, managed code is to manage all of the memory management (memory requests, memory releases, garbage collection, and so on) as the CLR of. NET, which means that some of the underlying operations are encapsulated with managed code and cannot be directly read in memory and hardware-related operations , the advantage is relatively safe, do not appear such as memory leaks and other problems, the shortcomings are also obvious, can not directly read the memory, the performance will be lost, sometimes used is not flexible.
Unmanaged is just the opposite, you can directly perform hardware operations, performance is relatively high, but the requirements for developers are also relatively high .
The most intuitive is that C # does not recommend using pointers, and C + + can use pointers to directly read memory;
C # uses garbage collection, C + + to manually dispose of objects ...
2. Strong type System
Strongly typed system (RTTI): (run-time type identification) run-time type recognition
A strongly typed system is a type of restriction on every data.
such as int sum=0;string str= "";
C #, C + + are strongly typed languages, and variables must be declared before they can be used.
JavaScript, however, is a weakly typed language.
Regulated code and strongly-typed systems