What does type-safe mean? How could a programming language be type-safe? Can you give an example to type-safe and non-type-safe languages? You will get the answers for all these questions in this post.
var int = 7;
var str = “7”;
var result = int + str;
Here numeric value 7 has been used as a string and concatenated with string “7” generating the string “77” as the result of this operation.
Consider this simple C# code below:
int mynumber = 3;
string mystring = “3”;
int result = mynumber + mystring;
The code above will not compile or run, displaying an error like Cannot implicitly convert type 'string' to 'int'.
That is why C# is a type-safe language. A type-safe language will not allow you mix different types of variables. Type-safe languages prevent you from making type errors.
C# is an exmple to type-safe languages.
So type safety is the extent to which a programming language discourages or prevents type errors. A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types for the program's constants, variables, and methods (functions), e.g., treating an integer (int) as a floating-point number (float). (wikipedia)
Hope this helps someone.