Type Safe in C#.NET step by step using example

There are many Programming languages out of that some are type safe while others are not like C#, Java are type safe so when coding using these languages developers are enforced to be type safe i.e. which does not allow to set one type of data with other type of data. It will not allow the written code to compile. Whereas languages like JavaScript are not type safe because they do allow you to set one type of data with other type of data i.e. they do not restrict user from doing so. Type safe is CLR feature and it comes under managed code.

Type Safe with JavaScript

Below is simple code on JavaScript which is having variables with two different data types one is “num” which represents numeric while other is “str” for string. And now we have to carry out “+” operation and want to see the result. So what would be the produced result?

  1. Whether it would be give us an error.
  2. If it allows to proceed then what would be the output produced, will it be sum 12+12=24.
  3. Or concatenation of the variable, 12+12 = 1212.

After you save the following JavaScript code written on Notepad, save it as .html or .htm and then open it on any browser to see what happens next?

Code will run on the browser popping up the output with concatenation which is “1212”.

So it is clear that JavaScript is a good language to code but it is not type safe as it allowed to produce the output of different data types “numeric” and “string”by doing concatenation without any restrictions.

Defining one type of data to other type of data unknowingly by the developer which causes bugs in the program or may lead to issue with unexpected results. If the language is Type Safe then such data type errors caused can be avoided.

Type Safe using C#

Now we will go and check the same using C# code on Visual Studio and check how does it reacts?

When a similar program is written using C# with different data type and then trying to setup one type of datatype with other datatype with the statement “int value = num + str;”.

It will show a red underline which means something is incorrect and after you hover your mouse it will show a message that “string” cannot be implicitly converted to “int”.

Being C# a type safe language of .NET platform you are stopped before you build your solution and run your program. C# ensures each syntax written is type safe and developers are prohibited before they land on error.

And if you ever want to convert datatype “string”to “int” that can be done using “convert” syntax then output would be sum of both the assigned value 12 + 12 = 24.


“Cast”in simple words is that moving of one datatype to other datatype and for that datatypes need to be defined. Consider a scenario where we have a “double” datatype and want to move to “int”. In such scenario datatype “double” moving to “int” datatype can be done but if double datatype carries a value which are in decimal then those values after decimal point will be dropped during datatype moving to “int”. This is because “int” which is integer has less precision i.e. integer does not support decimals compared to “db” double.

There are two types casting (a) Explicit Casting (b) Implicit Casting.

Explicit Casting

Datatypes when explicitly specified moving from one datatype to other datatype. Like in our above given example where we have “double” datatype and we want itto move to “integer” datatype is such type of casting is termed to be Explicit Casting.It requires casting operator means datatype need to mentioned. Following shown image depicts explicit casting where “double” datatype with value 200.90is moving to “int” datatype with value 200. Here decimal of “double” datatype values after decimal point which is “.90” will be dropped during datatype moving to “int”. So with explicit casting data loss will happen as you have seen loss of .90 on double value which is in actual 200.90.

Here explicit casting is denoted by below mentioned syntax “int” is integer with variable “num” and “db” is for double datatype: -

Int num = (int)db;

Implicit Casting

When doing Implicit Casting no need to define datatypes and also no need to give conversion for moving one type of datatype to other. Here there is no need to specify any casting(datatype) operator. This casting usually takes place when smaller datatype are moved to larger datatype. In C# it will not allow larger datatype to move smaller datatype and program will not move further on visual studio it will show red underline and can see error message when mouse hovered on red underline.

In the following image snapshot we have declared a double datatype with variable “dble” and assigning it integer with variable ”num”. This declared line is going to be allowed because smaller datatype “num” which is integer can moved to larger datatype “dble” which is double. After we have debugged the program integer value is assigned and shown on double datatype.

Hope that the terms type safe, casting, explicit casting, implicit casting explained with practical in this article is understood by the reader.

Go through below fresher’s C# project series which will be helpful to the one who is new to C# programming language: -

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more

Add a Comment