Difference between Type-Casting and Converting

Type-Casting:

In a simple word Type-Casting means converting parent type to child type.

Again what does it mean converting parent type to child type :

It means it saying that type casting can only be used when ever we see that their is an relationship between two class or between two classes.

for eg: we know that object class is the base class for any class .

So l store one value inside an object type :

object obj=”SURYA”;

string str=obj;   //Hear it gives an error that can not implicitly convert object type to string type.

 

To get rid of this error we have two solution that are :

soln 1) string str=Convert.ToString(obj);

soln 2) string str= obj as string ;

or

str=(string) obj;   //This is outdated not used any more.

The second solution is all about type casting ,this second solution hear applied because object class and string class has an relation.

So hear it is pretty clear that if we cast  classes then we can only go for type casting.

Converting:

When ever we deal with value type and we required cast then we go for Converting.

Conversion basically used when ever we cast one datatype to another datatype.

for eg: We want to convert string type to int type in this case if you want to go for type cast you get error because int (struct) type and string (Ref) type , hear if you are an c# programmer then to solve this problem you can use a method int.Parse which convert string to int.

eg: string str=”10″;

int i =int.Parse(str);

So From above we understand that when ever cast required for value type then we go for Conversion using different methods ,but when ever cast required for reference i.e class type then we go for Type-cast.

 

Hope this is useful..

If you didn’t satisfy this concept or any wrong is their then you free to comment on it.

Advertisements

2 thoughts on “Difference between Type-Casting and Converting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s