Type Safety In C#

Type safety is code that accesses only the memory locations it is authorized to access. A good type-safe programming language discourages or prevents type errors. A type error is a program behavior that causes a discrepancy between different data types. For example, if you have an integer variable and you assign it a floating number a type-unsafe would allow this to happen, but a type-safe would throw an error.

       public void TypeUnsafe()
        {
            int intValue = 0;
            float floatValue = 1.23f;

            intValue = floatValue; // error happens here
        }

The above code would produce the error “Cannot implicitly convert type ‘float’ to ‘int’. An explicit conversion exists (are you missing a cast?)” when it tries to compile. This is an example of C# enforcing type safety.