GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 388 of 436    Print  
What is the difference between Cast and Convert

int i1 = 5;
double d1 = i1 + 0.99;
int i2 = (int)d1; //result is 5
i2 = Convert.ToInt32(d1);//result is 6

Why does cast and convert in above example have different results.

  
Total Answers and Comments: 3 Last Update: May 28, 2008     Asked by: vsateeshk 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
July 11, 2007 07:59:34   #1  
samiksc Member Since: October 2005   Contribution: 233    

RE: What is the difference between Cast and Convert

Cast always works - truncates the number being cast to different data type
and may result into an invalid value. Cast operation will not throw exception.

e.g.

double d 123456789.1234;short i (short)d;

Console.WriteLine(i);

int j Convert.ToInt16(d);

Console.WriteLine(j);


The cast works and gives a meaningless negative value as output.
Convert.ToInt16 results into OverflowException being thrown.

Sometimes this is desirable since the errors generated due to wrong truncating
are hard to detect.

Other differences are some casts like casting from string to int is not allowed.
But Convert functions are available which do this conversion.


 
Is this answer useful? Yes | No
September 23, 2007 10:13:20   #2  
ChandimaP        

RE: What is the difference between Cast and Convert
Convert will correctly converts a number from one format to another if possible.
However Cast ignores the encoding format of the data type. It deals directly with the encoded binary values of the data. Therefore if the interpretation of the encoding of two data types are different a cast will make the value incorrect.

 
Is this answer useful? Yes | No
May 28, 2008 06:34:13   #3  
nikhilrawat23 Member Since: May 2008   Contribution: 15    

RE: What is the difference between Cast and Convertint i1 = 5;double d1 = i1 + 0.99;int i2 = (int)d1; //result is 5i2 = Convert.ToInt32(d1);//result is 6Why does cast and convert in above example have different results.
convert internally call parse method for conversion implicitly. If we are using convert for conversion and the object is null than it will return 0 value while if we r using cast then it will give null reference exception during compilation. String str null;Int temp convert.toint32 (str); //return 0 valueOr int temp (Int32) str; //throw exception during compilation

Converting performs a conversion -- it changes the VALUE returned (and often the type as well). Casting always returns the SAME value but as a different type.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape