CType() versus Convert.ToXXXX()

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

could someone please discuss the pros and cons of

CType(MyDouble,Decimal)

versus

Convert.ToDecimal(MyDouble)

.... and other such conversions ...
 
John said:
could someone please discuss the pros and cons of

CType(MyDouble,Decimal)

versus

Convert.ToDecimal(MyDouble)

... and other such conversions ...

They're fairly equivilent, but I would use CType, because you can be more
consistant; instead of casting between built-in types with Convert and other
types with CType, you can use CType for everything.
Convert is still needed for some things, though, such as
Convert.ToBase64String and other similar specialized methods.

- Pete
 
John A Grandy said:
could someone please discuss the pros and cons of

CType(MyDouble,Decimal)

versus

Convert.ToDecimal(MyDouble)

Convert is a function that accepts one data type, and returns another. CType
actually casts the binary data into a new type. So I assume that Convert has
more overhead, but this is just a very loose assumption. Did you check
msdn.microsoft.com, and the .NET documentation yet?


Jeremy
 
CType actually casts the binary data into a new type

I don't think this is true. In most cases, CType and Convert.Toxxx do pretty
much the same thing. For example if you look at the IL for the following:

---------------

Dim a as Integer = 5
Dim b as string

b = cstr(a) ' Method 1
b = Convert.ToString(a) ' Method 2

---------------

You'll see that both end up calling System.Int32::ToString() if you trace
through the call stack.

If CType casted the binary data, then you wouldn't get a string containing
"5", you would get a string contining the character with numeric value 5
which isn't the same thing.

In some cases, CType behaves like directcast, but it is better to use
directcast when you can (when the run time types are the same). See other
posts in this group involving "CType vs DirectCast" etc.

IMO, Ctype behaves like a shortcut to Convert.Toxxx with extra added
features such as casting (like directcast).

HTH,

Trev.
 
Trev Hunter said:
I don't think this is true. In most cases, CType and Convert.Toxxx do pretty
much the same thing. For example if you look at the IL for the following:

So what is the case when you cast to something other than a string?
 
So what is the case when you cast to something other than a string?

It depends on the conversion, sometimes an optimized cast is allowed (e.g.
integer to long), sometimes overflow checking has to be done. AFAIK, ctype
allows the compiler to preform optimizations when it can, but an explicit
call to Convert.Toxxx is always predefined - so no optimizations can be done
by the compiler.

HTH,

Trev.
 
As a side note, unless you're doing really heavy looping or intensive math,
there won't be any noticable difference between any of the methods.

If you are doing heavy looing or intensive math then you sholuld try and
limit your castings / conversions rather than rely on compiler
optimizations.

Just my 2c

Trev.
 
Trev Hunter said:
It depends on the conversion, sometimes an optimized cast is allowed (e.g.
integer to long), sometimes overflow checking has to be done. AFAIK, ctype
allows the compiler to preform optimizations when it can, but an explicit
call to Convert.Toxxx is always predefined - so no optimizations can be done
by the compiler.

Interesting, still seems to me that Convert is the weaker of the two. But
again, my opinion is just based on assumption.

Jeremy
 
Well, the only really weakness is that it involves extra function calls
rather than doing inline processing. Apart from that it's essentially doing
the exact same thing. I'm not sure, but I assume that the JIT compiler is
also capable of inlining the Convert.Toxxx functions to further optimize
(which would make it exactly the same as ctype).

I guess another plus point of ctype is that you can shorten the amount you
have to type by using one of the other "c" functions (cint, cstr etc.) which
in most cases are the came as their ctype equivalent.

As mentioned in my other post, performance at this level is really not all
that relevant to a typical application IME.

HTH,

Trev.
 
Back
Top