Cast or Convert?
-
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 forConvert.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
-
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 forConvert.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
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:
-
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 forConvert.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
-
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:
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 aboutToString()
. I've just extended that to theConvert
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
-
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 forConvert.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
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.
-
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 forConvert.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
-
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 forConvert.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