Difference between type cast and Convert.int32 ?

Can any one explain me the difference between the below 2 statements.

status = (int)InvoiceStatus.VALID;

status = Convert.ToInt32(InvoiceStatus.VALID);

Which is better and faster? And when should I use which?
GouthamAnandAsked:
Who is Participating?
 
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
go through the following link - good discussion for cast vs convert
http://bytes.com/forum/thread266654.html
0
 
Leon TealePenetration TesterCommented:
They pretty much do the same thing.  If you're just changing data from one form to another for use, such as changing a "45" from a varchar to an int, use CAST; Convert offers more options than cast which may lead to more processing time.  And by this, I of course mean MINIMAL, not noticeable processing time.  Convert allows you to add a format style to your convertion, if you want it to.  For example, if you're changing a date to a string, you can say which part of the date to return instead of returning the whole thing and making .NET format it.
0
 
BinuthCommented:
Casting will throw an exception if it fails or the input is null. The Convert class will return 0 if the input is null.
0
 
ajollyCommented:
difference will depend on the type of variable2.

For instance, if InvoiceStatus.VALID has a value which is a string reference to
"23" then Convert.ToInt32 will parse the int. However, you can't *cast*
from a string to an int.
0
 
GouthamAnandAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.