What Does Type-Safe Mean?/ What is a Type-Safe Language? (C#)

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.

Consider this simple javascript code below:

var int = 7;
var str = “7”;
var result = int + str;
alert (result);

When you run this code, alert displays the value 77.  Why is this happening? Is this what you expected? This happened because javascript is not type-safe. You can assign one type of data to another type of data in javascript.

Javascript is an example to non-type-safe languages.

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.

Good luck,

One thought on “What Does Type-Safe Mean?/ What is a Type-Safe Language? (C#)

  1. Because the admin of this web page is working, no hesitawtion very sooon it
    will be famous, due to its feature contents.

Leave a Reply

Your email address will not be published. Required fields are marked *