What is Type safety in .NET?

Type-safe code accesses only the memory locations it is authorized to access. For example, type-safe code cannot read values from another object’s private fields. It accesses types only in well-defined, allowable ways. If we want to work directly with memory addresses and can manipulate bytes at these addresses then we have to declare that code chunk as unsafe using the unsafe Keyword in C#. So that CLR will not do any extra verification on this code.

Actually during just-in-time (JIT) compilation, an optional verification process examines the metadata and Microsoft intermediate language (MSIL) of a method to be JIT-compiled into native machine code to verify that they are type safe. This process is skipped if the code has permission to bypass verification. For example, the runtime cannot prevent unmanaged code from calling into native (unmanaged) code and performing malicious operations. When code is type safe, the runtime’s security enforcement mechanism ensures that it does not access native code unless it has permission to do so. All code that is not type safe must have been granted Security Permission with the passed enum member Skip Verification to run.
Tags: ,
Hot on Web:


About author