Type-Safety in C#

At the first, what is type safe code and why is it relevant to secure programming? Type safety is certainly one of the most confusing aspects for somebody learning .Net. When .Net was released, it had a tag attached claiming that the programs developed in .Net are more stable when compared to programs developed using VB or VC++. This just adds to the confusion if you do not understand type safety very well.
Type safety
Type safe code can access only the memory locations that it has permission to execute. Type safe code can never access any private members of an object. Type safe code ensures that objects are different from each other and are therefore safe for unplanned or unwanted ambiguity.
CLR performs a mandatory type safety check, called verification, during JIT compilation. This is done by a tool called peverify.exe, which examines the Microsoft Intermediate Language and the metadata included in the assembly.
If an assembly (or code) is successfully verified as type safe, it is called verifiably type safe code. The code is said to be verifiably type safe when any references to the types inside that code are strictly compatible with the types they are referring to.
Code need no always be verifiably type safe. This is for the situations where you have both managed as well as unmanaged code in the same assembly. Remember, unmanaged code need no always be un-verifiable code.
Even though this is a mandatory process which is performed by the CLR, it can be skipped by providing necessary permissions to that assembly. CLR ensures that the type safe code does not end up in any unwanted situations like calling native or unmanaged code or performing any unwanted operations.
Scroll to Top