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. The Lounge
  3. Cast or Convert?

Cast or Convert?

Scheduled Pinned Locked Moved The Lounge
csharpc++comalgorithmsquestion
7 Posts 6 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.
  • M Offline
    M Offline
    Miszou
    wrote on last edited by
    #1

    Following on from Rama's post[^], I just realized that I very rarely cast anything in C# - I use the Convert class or ToString() instead. In C/C++ I was casting all over the place, but not so much in C#. IMHO it is easier to find conversions in your code by searching for Convert.To than it is to find all possible conversions by looking for "things" inside parentheses... Is there a preferred method, or is it just a style preference? Is casting faster than Convert? Is it better to have easily searchable code or more performant code? Just as a corollary, I always liked the C++ casting operators, static_cast, dynamic_cast, reinterpret_cast etc, because it made it easier to search for cast operations, and you couldn't just cast things about all over the place without thinking about the consequences.

    The StartPage Randomizer - The Windows Cheerleader - Twitter

    P Z D C L 5 Replies Last reply
    0
    • M Miszou

      Following on from Rama's post[^], I just realized that I very rarely cast anything in C# - I use the Convert class or ToString() instead. In C/C++ I was casting all over the place, but not so much in C#. IMHO it is easier to find conversions in your code by searching for Convert.To than it is to find all possible conversions by looking for "things" inside parentheses... Is there a preferred method, or is it just a style preference? Is casting faster than Convert? Is it better to have easily searchable code or more performant code? Just as a corollary, I always liked the C++ casting operators, static_cast, dynamic_cast, reinterpret_cast etc, because it made it easier to search for cast operations, and you couldn't just cast things about all over the place without thinking about the consequences.

      The StartPage Randomizer - The Windows Cheerleader - Twitter

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

      Convert sucks... up cycles; don't use it. Why use a method when an operator will suffice?

      Miszou wrote:

      it is easier to find conversions in your code by searching for Convert.To

      I'll agree with that, but I still won't use Convert. Why would anyone search for casts anyway? :confused:

      M 1 Reply Last reply
      0
      • M Miszou

        Following on from Rama's post[^], I just realized that I very rarely cast anything in C# - I use the Convert class or ToString() instead. In C/C++ I was casting all over the place, but not so much in C#. IMHO it is easier to find conversions in your code by searching for Convert.To than it is to find all possible conversions by looking for "things" inside parentheses... Is there a preferred method, or is it just a style preference? Is casting faster than Convert? Is it better to have easily searchable code or more performant code? Just as a corollary, I always liked the C++ casting operators, static_cast, dynamic_cast, reinterpret_cast etc, because it made it easier to search for cast operations, and you couldn't just cast things about all over the place without thinking about the consequences.

        The StartPage Randomizer - The Windows Cheerleader - Twitter

        Z Offline
        Z Offline
        ziwez0
        wrote on last edited by
        #3

        actually i find myself thinking about this yesterday, i always use convert...

        1 Reply Last reply
        0
        • P PIEBALDconsult

          Convert sucks... up cycles; don't use it. Why use a method when an operator will suffice?

          Miszou wrote:

          it is easier to find conversions in your code by searching for Convert.To

          I'll agree with that, but I still won't use Convert. Why would anyone search for casts anyway? :confused:

          M Offline
          M Offline
          Miszou
          wrote on last edited by
          #4

          It's not "Troll food". It's a continuation of a train of thought that was started by this[^] post. I just thought it might be worth getting some other opinions on the matter. After all, whats the difference between these two lines? string s1 = datarow["name"].ToString(); int n1 = Convert.ToInt32( datarow["number"] ); when you could just as easily do this: string s2 = (string)datarow["name"]; int n2 = (int)datarow["number"]; The original post was asking about ToString(). I've just extended that to the Convert class. And why would anyone search for conversions? There are a few occasions that I can think of, mostly due to underlying data changes and the need to change the calling code. However, the purpose of the original post was to discuss the pros/cons of performance over readability or vice versa.

          The StartPage Randomizer - The Windows Cheerleader - Twitter

          1 Reply Last reply
          0
          • M Miszou

            Following on from Rama's post[^], I just realized that I very rarely cast anything in C# - I use the Convert class or ToString() instead. In C/C++ I was casting all over the place, but not so much in C#. IMHO it is easier to find conversions in your code by searching for Convert.To than it is to find all possible conversions by looking for "things" inside parentheses... Is there a preferred method, or is it just a style preference? Is casting faster than Convert? Is it better to have easily searchable code or more performant code? Just as a corollary, I always liked the C++ casting operators, static_cast, dynamic_cast, reinterpret_cast etc, because it made it easier to search for cast operations, and you couldn't just cast things about all over the place without thinking about the consequences.

            The StartPage Randomizer - The Windows Cheerleader - Twitter

            D Offline
            D Offline
            Dave Parker
            wrote on last edited by
            #5

            Think I usually use casting if the source really is the destination type and I'm unboxing (such as something I know is an Int32 but I have a reference of type Object), Convert if the source type is something else such as a different sized integer other than a string and Parse if the source is a string. If it was something time critical than I'd probably use casting more as I'd guess it's faster, though 99% of the code I write isn't.

            1 Reply Last reply
            0
            • M Miszou

              Following on from Rama's post[^], I just realized that I very rarely cast anything in C# - I use the Convert class or ToString() instead. In C/C++ I was casting all over the place, but not so much in C#. IMHO it is easier to find conversions in your code by searching for Convert.To than it is to find all possible conversions by looking for "things" inside parentheses... Is there a preferred method, or is it just a style preference? Is casting faster than Convert? Is it better to have easily searchable code or more performant code? Just as a corollary, I always liked the C++ casting operators, static_cast, dynamic_cast, reinterpret_cast etc, because it made it easier to search for cast operations, and you couldn't just cast things about all over the place without thinking about the consequences.

              The StartPage Randomizer - The Windows Cheerleader - Twitter

              C Offline
              C Offline
              Chris Maunder
              wrote on last edited by
              #6

              I use the as operator, where possible, instead.

              cheers, Chris Maunder

              CodeProject.com : C++ MVP

              1 Reply Last reply
              0
              • M Miszou

                Following on from Rama's post[^], I just realized that I very rarely cast anything in C# - I use the Convert class or ToString() instead. In C/C++ I was casting all over the place, but not so much in C#. IMHO it is easier to find conversions in your code by searching for Convert.To than it is to find all possible conversions by looking for "things" inside parentheses... Is there a preferred method, or is it just a style preference? Is casting faster than Convert? Is it better to have easily searchable code or more performant code? Just as a corollary, I always liked the C++ casting operators, static_cast, dynamic_cast, reinterpret_cast etc, because it made it easier to search for cast operations, and you couldn't just cast things about all over the place without thinking about the consequences.

                The StartPage Randomizer - The Windows Cheerleader - Twitter

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Set things up so casting generates a warning, Convert.To will hide that.

                Visit http://www.notreadytogiveup.com/[^] and do something special today.

                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