What is type safety in .net?

Type safety prevents assigning a type to another type when are not compatible.

publicclass Employee{}

publicclass Student{}

In the above example, Employee and Student are two incompatible types. We cannot assign an object of employee class to Student class variable. If you try doing so, you will get an error during the compilation process.

In the above example, Engineer and Accountant class derives from the same Employee base class. When tried to type cast object of Accountant class to Engineer class variable, it throws System.InvalidCastException at runtime:

Unable to cast object of type 'Accountant' to type 'Engineer'.

Above type checking happens at runtime, hence it is called runtime type checking.

When does the type safety check happens and who ensures the type safety in .net?

As discussed type safety check happens both at Compile time and at runtime. The compiler will do the compile-time type safety check and CLR will do the runtime safety check.

How type safety checks makes developers life easy?

The advantages type safety are pretty straightforward.

At compile time, we get an error when a type instance is being assigned to an incompatible type; hence preventing an error at runtime. So at compilation time itself, developers come to know such errors and code will be modified to correct the mistake. So developers get more confidence in their code.

Run time type safety ensures, we don't get strange memory exceptions and inconsistent behavior in the application.