Type Safe in C#.NET step by step using example
- Whether it would be give us an error.
- If it allows to proceed then what would be the output produced, will it be sum 12+12=24.
- Or concatenation of the variable, 12+12 = 1212.
Code will run on the browser popping up the output with concatenation which is “1212”.
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.
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;
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: -