468,491 Members | 1,918 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

CType() versus Convert.ToXXXX()

could someone please discuss the pros and cons of

CType(MyDouble,Decimal)

versus

Convert.ToDecimal(MyDouble)

.... and other such conversions ...
Nov 20 '05 #1
8 6953
John A Grandy wrote:
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
Nov 20 '05 #2
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #3
> 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.

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:Ds********************@twister.tampabay.rr.co m...
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #4
"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
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:


So what is the case when you cast to something other than a string?
Nov 20 '05 #5
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.

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:yE******************@twister.tampabay.rr.com. .. "Trev Hunter" <hu*********@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
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:
So what is the case when you cast to something other than a string?

Nov 20 '05 #6
"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.


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

Jeremy

Nov 20 '05 #7
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.
Nov 20 '05 #8
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.
"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:Ma******************@twister.tampabay.rr.com. ..
"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.


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

Jeremy

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joeri | last post: by
1 post views Thread by Logan X via .NET 247 | last post: by
2 posts views Thread by Jarod_24 | last post: by
7 posts views Thread by Brian Henry | last post: by
8 posts views Thread by =?Utf-8?B?d2lubGlu?= | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.