Introduction
In this blog, we will try to understand the type-safety, implicit and explicit typecasting. When we talk about safety, it means that we are trying to prevent some type of accident. In the real world, we have two kinds of people, one who believes in safety and another one is enjoying life. In the same way, we have two types of language one is type-safe another one is not.
For example, Javascript is not type-safe. Please check out the below code.
- <script>
- var num=5;//numeric
- var str="5";//string
- var value=num+str;
- alert(value);
- </script>
I am trying to write the same code in C#. Please check the below code and the intelligence from the compiler.
- int num = 5;
- string str = "5";
- int value = num + str;
Let's discuss on Casting
Please check the below code.
- double db = 100.20;
- int num = db;
In case you need to achieve this one you can use convert function and casting. Please check the below code casting. Casting means moving data from one data type to another data type.
Explicit type casting
The below code refers to the explicit casting, which means we need to specify basically which data type you want to convert.
- double db = 100.20;
- int num = (int)db;
Implicit casting means from one data type to another data type without giving any type of conversion. In the below code, I am moving the integer value to double data type.
- double db = 100.20;
- int num = (int)db;
- db = num;//implicit
Summary
In this blog, we have discussed the Typesafe and casting with a simple program.

Join the conversation! Your thoughts help the community grow.