Type Casting in C#

Salesforce Admin
Study Material

Datatype conversion/ type casting

-DataType conversion happens when we assign the value of one data type to another. If the data types are compatible, then compiler does Implicit Type Conversion. If not comparable, then they need to be converted explicitly which is known as Explicit Type conversion.
-Implicit conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types.
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9
-Explicit conversions : Explicitly informing the compiler that you intend to make the conversion and that you are aware that data loss might occur, or the cast may fail at runtime.
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
Console.WriteLine(myDouble); // Outputs 9.78
Console.WriteLine(myInt); // Outputs 9
-Helper class conversion : We can use System.Convert class or the parse methods of built in datatypes
int myInt = 10;
double myDouble = 5.25;
bool myBool = true;
Console.WriteLine(Convert.ToString(myInt)); // convert int to string
Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int
Console.WriteLine(Convert.ToString(myBool)); // convert bool to string

Boxing And Unboxing

Basically, Boxing converts a Value Type variable into a Reference Type variable, and Unboxing achieves the vice-versa. Boxing and Unboxing enable a unified view of the type system in which a value of any type can be treated as an object.

-The process of converting a Value Type variable (char, int, etc.) to a Reference Type variable (object) is called Boxing.
-Boxing is an implicit conversion process in which object type (supertype) is used.
-Value Type variables are always stored in Stack memory, while Reference Type variables are stored in Heap memory.
Example :
int num = 23; // 23 will assigned to num
Object Obj = num; // Boxing

First, we declare a Value Type variable num of the type int and initialize it with value 23. Now, we create a Reference Type variable obj of the type Object and assign num to it. This assignment implicitly results in the Value Type variable num to be copied and stored in Reference Type variable obj as shown below figure :


-The process of converting a Reference Type variable into a Value Type variable is known as Unboxing.
-It is an explicit conversion process.
Example :
int num = 23; // value type is int and assigned value 23
Object Obj = num; // Boxing
int i = (int)Obj; // Unboxing

Description: We declare a Value Type variable num, which is of the type int and assign it with integer value 23. Now, we create a Reference Type variable obj of the type Object, in which we box the variable num. Now, we create a Value Type integer i to unbox the value from obj. This is done using the casting method, in which we explicitly specify that obj must be cast as an int value. Thus, the Reference Type variable residing in the heap memory is copied to stack as shown in below figure :

Program to Practice
-Practice implicit and explicit conversion.
-Int to long
-Char to String
-Practice Boxing and Unboxing
-C# program to Convert Feet to Meter