Type Casting in C# .Net
Typecasting is a concept of converting from one data type into another data type. C# supports two types of typecasting
a) Implicit type casting under control of CLR Lower to higher
b) Explicit type casting under the control of programmer higher to lower
Observations
Byte to long implicit
Int to byte Explicit
Int to float implicit
Float to int explicit
C # supports 4 types of Explicit type casting technique
C ++ style of typecasting Converting Boxing and unboxing Parsing
These core tutorials will help you to learn the fundamentals of .NET. For an in-depth understanding and practical experience, explore Online ".NET Training"
Working with C++ style of typecasting
Ex:-
int I = 10;Byte b = (byte) I;
Syntax
DT1 V1 = value;
DT2 V2 = (DT2) V1;
Example of C++ Style of typecasting
Open console application project
Write the following code in the main method
Int I = 150;Byte Sal = (byte) I;
Console. Write Line (Sal);
Console. Read Line ();
Execute the project (f5) à 150
Observations
In the C++ style of typecasting, there is a possibility of loading the data.
Convert
Convert is a predefined class, which is the part of F
Working with convert class is called as “Converting”
The class contains a set of methods.
Predefined methods on convert class
String x = 123
Byte b = convert. To Byte (X);
Convert. To Byte (value)
Convert. To char (value)
Convert. To string (value)
Convert. To Boolean (value)
Convert. To Float (value)
* * * *
Convert. To INT 16()…………….. for short
Convert. To INT 32()…………….. for int
Convert. To INT 46()…………….. for long
Example of converting
Open Console application project
Write the following code in the main method
Class Program{
Static void main (string [] args)
{
For (int I = 0;I < = 255; I ++)
{
Char c = Convert. To char (i);
Console. write (c);
}
Console . Read line ();
}
}
F5
Working with Boxing and Un-Boxing
Boxing is a concept of converting value type in to reference types.
UnBoxing is a concept of converting reference type in to value types.
Note: Console application is specially designed for a character-based operating system like DOS
As of now, CLR software is not yet introduced for DOS OS, hence console Applications are not Executable in DOS OS.
Console Application contains only LOGIC. but not GUI
In order to develop GUI, Applications Microsoft introduced the “Windows forms application” template.