about casting

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
Your answer

:-)

Last reply Jon, feel free to make a last, but I won't reply in this thread
anymore.

Cor
 
(string) X, is in my ide more or less the same like CType(X,string)
while I see
X as string, the same as DirectCast(X, string)

I would interpret that in reverse:

I would think (string) x is more equivelent to DirectCast(x, String)

and

x as string is more like CType(x, String)

-Scott
 
I would interpret that in reverse:

I would think (string) x is more equivelent to DirectCast(x, String)

and

x as string is more like CType(x, String)

But that's still wrong:
1) A cast expression in C# does more than DirectCast in VB (but less
than CType, I think)
2) x as string will never throw an exception, whereas CType can.

As I've said before, "as" is the same as TryCast as far as I can tell.

Jon
 
Yes, I get that Jon, but I'm not really thinking about exceptions here since
in VB, both DirectCast and CType can throw exceptions. I'm really talking
more in terms of performance when the cast is possible.
 
Scott M. said:
Yes, I get that Jon, but I'm not really thinking about exceptions here since
in VB, both DirectCast and CType can throw exceptions. I'm really talking
more in terms of performance when the cast is possible.

If the cast is possible as an "inheritance-style" cast, both "as" and
direct casting are like DirectCast.

If a user-defined operator conversion is involved then direct casting
acts more like CType - but that's pretty rare, and the operator to call
is still decided at compile time.

I get the impression that CType is capable of deciding which conversion
to apply (like parsing a string to an integer) at exection time. That's
not the case with either direct casting or "as" in C#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top