What are the differences between casting a result using (decimal) and doing Convert.ToDecimal.
Both seem to generate the same result...so what should I use, is there any particular circunstance
Tanks a lot!
What are the differences between casting a result using (decimal) and doing Convert.ToDecimal.
Both seem to generate the same result...so what should I use, is there any particular circunstance
Tanks a lot!
Casting using (decimal) or Convert.ToDecimal ? What to use?
Edi-Mobixell
No, it doesn't throw an exception. It gives a compile-time error.
Kidsauth
Well, convert will almost always work. Its a Static Method of the convert class and has overloads for just about all of the basic value types. casting will only work from/to certian types that are compatible.
For example....
string s="12345678";
int i=(int)s; //throws an exception
string and integer do not have a common base class, therefore, some serious work has to be done in order to turn the 16-byte unicode string into a 4-byte integer so trying to tell the runtime to just look at that same memory address and read it as an integer generates an exception....which is good because if this worked, you'd actually end up with a negative integer of a magnitude of around 2 billion, based on the memory would look. So you HAVE to use convert here.
The only time that I've seen convert cause problems is when people stop performing validation in their code in the appropriate places by saying, "Why validate any inputs (not just UI inputs either) when I can always just call convert!" ...not good...
So cast when you can. Its only a tiny bit faster, but it does force you to write things cleaner. Where appropriate, use convert....but use it as soon as you can in a process...the deeper down in your code you convert values into the appropriate target types, the more chances for exceptions to be thrown.
Penicillin
So the rule would be:
Use explicit cast whenever possible, since Convert method does not help either to loose information in case of "narrowing casting". Only use Convert in those specific cases where u have to do radical changes, as in converting a string to a number.
Right
Wilton Kwok
Hi
(decimal) is a casting operator and Convert.ToDecimal is a static method. That is the basic difference. So the casting operator has limitations for converting from a source type, but Convert.ToDecimal has a lot of overloads. So to answer your question, for simple numeric conversions, it is ok to use (decimal) and when converting from a string or other types, use the Convert.ToDecimal.
Regards
Seth Rippee
Convert is used for values that cannot be explicitly cast to another value and implement the IConvertable interface.
mate6666
Well lets then reformulate the rule:
"Use cast whenever u can, as long as there is no converting problem due to the casting. If nothing hinders to use Explicit Cast, then we should use it rather than Convert, either because of its faster performance (though not that much) or to its code clarity..."
Right