Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Convert or Parse

Convert or Parse

Scheduled Pinned Locked Moved C#
tutorial
7 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lodeclaw
    wrote on last edited by
    #1

    I was told that the most common method to convert a value is to use the Parse method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example... Convert.ToInt32(value); or int.Parse(value.ToString()); Thanks guys!

    L A P G 4 Replies Last reply
    0
    • L Lodeclaw

      I was told that the most common method to convert a value is to use the Parse method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example... Convert.ToInt32(value); or int.Parse(value.ToString()); Thanks guys!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, it depends on the Type of your value: if value is a string, then value.ToString() is redundant, and both Convert and int.Parse would behave identically (in fact one is calling the other). if value is some kind of integer to begin with, Convert.ToInt32() is much faster, since no conversion from integer to string, and no conversion from string to int are involved. if value is something else (a DateTime, a user-defined type, ...) Convert will not accept it, whereas the value.ToString() result may or may not look like an integer, and be acceptable to int.Parse :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      modified on Sunday, June 12, 2011 8:02 AM

      1 Reply Last reply
      0
      • L Lodeclaw

        I was told that the most common method to convert a value is to use the Parse method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example... Convert.ToInt32(value); or int.Parse(value.ToString()); Thanks guys!

        A Offline
        A Offline
        akidan
        wrote on last edited by
        #3

        Luc summed it up well, but I just wanted to add one other non-obvious difference. With value = null; Convert.ToInt32(value) returns 0 int.Parse(value.ToString()) throws NullReferenceException. int.Parse(value) throws ArgumentNullException. (if value is String)

        F 1 Reply Last reply
        0
        • A akidan

          Luc summed it up well, but I just wanted to add one other non-obvious difference. With value = null; Convert.ToInt32(value) returns 0 int.Parse(value.ToString()) throws NullReferenceException. int.Parse(value) throws ArgumentNullException. (if value is String)

          F Offline
          F Offline
          freakyit
          wrote on last edited by
          #4

          hi, look at the int.TryParse(object,out bool) functionality. The boolean value stands up for true (parseable) and false(not parseable) about trying to parse the value. There will be no exception raised. if you want to use int.Parse instead of TryParse you need to catch the exception with try catch.. bless :)

          1 Reply Last reply
          0
          • L Lodeclaw

            I was told that the most common method to convert a value is to use the Parse method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example... Convert.ToInt32(value); or int.Parse(value.ToString()); Thanks guys!

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            In my opinion the only worthwhile member of Convert is System.Convert.ChangeType, will that do what you want? Otherwise, maybe a simple cast is what you want? Or, if it's a type of your own creation, perhaps an implicit conversion operator? You would have to give us more information.

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              In my opinion the only worthwhile member of Convert is System.Convert.ChangeType, will that do what you want? Otherwise, maybe a simple cast is what you want? Or, if it's a type of your own creation, perhaps an implicit conversion operator? You would have to give us more information.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Hijack If you have 2 classes that are identical (set of credentials) can you use System.Convert.ChangeType to convert one to the other?

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • L Lodeclaw

                I was told that the most common method to convert a value is to use the Parse method, however it only works with strings, as you all know... So I was wondering, then: if you're converting a non-string value to another non-string, is it better/more efficient to use for example... Convert.ToInt32(value); or int.Parse(value.ToString()); Thanks guys!

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                The most common method to convert a non-string value to an int is implicit or explicit casting. Example:

                // implicit casting:
                byte b = 42;
                int answer = b;

                // explicit casting:
                double d = 42.0;
                int answer = (int)d;

                Despite everything, the person most likely to be fooling you next is yourself.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups