Difference between type cast and Convert.int32 ?

Posted on 2008-11-04
Last Modified: 2012-06-22
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?
Question by:GouthamAnand
    LVL 6

    Expert Comment

    by:Leon Teale
    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.
    LVL 14

    Expert Comment

    Casting will throw an exception if it fails or the input is null. The Convert class will return 0 if the input is null.
    LVL 7

    Expert Comment

    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.
    LVL 26

    Accepted Solution

    go through the following link - good discussion for cast vs convert

    Author Closing Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now