Thursday , December 14 2017
Home / Dot Net / C# / Lesson 5: TypeCasting or Type-Conversion in C#

Lesson 5: TypeCasting or Type-Conversion in C#

TypeCasting or Type-Conversion in C#:

Programming in any language will involve manipulation data. For Accessing Memory we use variables. We can create specific type of variable to access some data. C# allows us to create variables of many types but, being a statically typed language, it does not allow us to assign the value of one type of variable into another type of variables.

For Example

Here It is Possible because both a and b are same datatypes it means both source and destination datatypes are same

But in some cases source and destination datatypes are not same like below Example

at that type we will get an exception like InvalidCastException with below message

Cannot implicitly convert type ‘long’ to “int?”?

So at that time We have to Cast data type.

Typecasting:

Type Casting or Type Conversion is a mechanism to convert one data type value to another one. Type conversion is possible if both the data types are compatible to each other; otherwise you will get an InvalidCastException.

Type Casting are of two types

1.Implicit Type Casting

2.Explicit Type Casting

1.Implicit conversion:

Implicit conversion is done by compiler automatically. It includes conversion of a smaller data type to a larger data types and conversion of derived classes to base class. This is also called as safe type conversion.

Rules:

Source DataType and Destination Data Types are of Same Types and Destination datatype should be greater than source datatype

Example1:

here long l is destination datatype

and int a is source data type

here both are same types and destination datatype(long) is greater than source type(int)

so this casting will done by compiler at Compilation time

Example 2:

here long l is source datatype

and int a is destination data type

here both are same types but source datatype(long) is greater than destination type(int)

so here typecasting wont perform it throws compile time error For doing this type of casts we use explicit type casting

2.Explicit conversion:

Explicit conversion is done by using a cast operator. It includes conversion of larger data type to smaller data type and conversion of base class to derived classes. In this conversion information might be lost or conversion might not be succeed for some reasons. This is also called as un-safe type conversion.

Example 1:

Output:

120

 

Example 2:

Output:

It will get an Exception(System.OverFlowException) called {“Value was either too large or too small for an Int32.”}

Because in Compile Time Compiler Casts but at the time of Execution The value of long l exceeds  the range of Integer.

Explict Casting Will Perform in 3 Ways:

Way-1 By Using Convert Class:

Way-2 By Using Parse Method:
or
Way-3 By Using DataType or Class:
Output:

25

Back To C# Complete Tutorials

Thanks for reading this article and if you like this please share with your Friends.

Print Friendly, PDF & Email

Give us your valuable feedback

comments

About Murali

Hello everyone I am Software Developer having 3+ years of Experience in Microsoft Technologies and Others.

My skills :- Asp.net, C#,Ado.net, SQL SERVER, web services, windows Service,Windows applications,HTML,CSS,
JavaScript,JQuery,AnjularJs

Check Also

studyclues_c_object

Lesson 8: Object In C#

c# object: A class and an object are different things. A class defines a type of …

%d bloggers like this: