Jarodtweiss
asked on
Difference between DirectCast and CType
Hello
If I want to resume the differences, as explained in the MSDN:
- CType can be used for narrowing or widening conversion
- to use DirectCast, there must be an heritance or implementation relation between the classes
- DirectCast could be quicker because it doesn't use the Visual Basic run-time helper routines for conversion.
First question : what is the "Visual Basic run-time helper routines for conversion" ?
Second question : in the version 1.0 and 1.1 (the help has been modified in 2.0) it was specified that the dynamic type of the two objects must be identical to have DirectCast succeeds, whereas it is only specified a relation in inheritance or implementation in 2.0. But if you look to the generated MSIL code (I have looked in 2.0), you will notice that both classes are compiled using the directive "castclass"
So I wonder how one can be quicker that the second one if they are compiled in the identical code ?
Thanks!
If I want to resume the differences, as explained in the MSDN:
- CType can be used for narrowing or widening conversion
- to use DirectCast, there must be an heritance or implementation relation between the classes
- DirectCast could be quicker because it doesn't use the Visual Basic run-time helper routines for conversion.
First question : what is the "Visual Basic run-time helper routines for conversion" ?
Second question : in the version 1.0 and 1.1 (the help has been modified in 2.0) it was specified that the dynamic type of the two objects must be identical to have DirectCast succeeds, whereas it is only specified a relation in inheritance or implementation in 2.0. But if you look to the generated MSIL code (I have looked in 2.0), you will notice that both classes are compiled using the directive "castclass"
So I wonder how one can be quicker that the second one if they are compiled in the identical code ?
Thanks!
ASKER
Hello
thanks for your explanation.
However, it is not really what I was asking for. I have also explained this difference in my question (with less precise words, I agree).
What I was asking is how can we a better run time performance with DirectCast when the two casts are compiled in MSIL using the same keyword "castclass".
I have checked in the ECMA documentation abot this keyword but nothing special is said. No extra metadata is generated, nothing that can let us think that the two casts will behave differently.
I have done some performance measuremenent and there is a difference. DirectCast is sometimes quicker, sometimes not, depending of the complixiy of the class. But how can this be possible if the two casts are compiled the same way ? They should be executed the same way !
thanks for your explanation.
However, it is not really what I was asking for. I have also explained this difference in my question (with less precise words, I agree).
What I was asking is how can we a better run time performance with DirectCast when the two casts are compiled in MSIL using the same keyword "castclass".
I have checked in the ECMA documentation abot this keyword but nothing special is said. No extra metadata is generated, nothing that can let us think that the two casts will behave differently.
I have done some performance measuremenent and there is a difference. DirectCast is sometimes quicker, sometimes not, depending of the complixiy of the class. But how can this be possible if the two casts are compiled the same way ? They should be executed the same way !
CType slower than DirectCast
Use the DirectCast keyword instead of the CType keyword when you're just
casting an object variable to a variable of a different type in .NET.
DirectCast can deliver better performance because it never calls methods in the
Microsoft.VisualBasic DLL. You should notice the risk of using DirectCast,
though. DirectCast can raise a run-time error if the run-time types don't
match. In this case, CType may perform type coercion, while DirectCast raises
an error.
Use the DirectCast keyword instead of the CType keyword when you're just
casting an object variable to a variable of a different type in .NET.
DirectCast can deliver better performance because it never calls methods in the
Microsoft.VisualBasic DLL. You should notice the risk of using DirectCast,
though. DirectCast can raise a run-time error if the run-time types don't
match. In this case, CType may perform type coercion, while DirectCast raises
an error.
ASKER
You say "DirectCast can deliver better performance because it never calls methods in the Microsoft.VisualBasic DLL"
I agree with you, it is probably what MS means when they said that it doesn't use the Visual Basic run-time helper routines for conversion
And I can imagine that extra checks are performed during the compilation into MSIL. But what is executed by the CLR ? The code compiled by the JIT (or NGEN) from the MSIL.
Sorry if I insist on that point but let's take another example and the differences between Convert.ToInt32 and Conversions.ToInteger (introduced in .NET FX 2.0). In that case, the two methods are converted to something different in MSIL. So I do not have any problem to admit than the Conversions class is doing something else (even if I assume that it is doing a simple delegation to provide to the VB developpers the ability to work with VB types instead of CLR types)
To sum up, how can the same MSIL code (produced either by CType or DirectCast) be executed differently ?
I agree with you, it is probably what MS means when they said that it doesn't use the Visual Basic run-time helper routines for conversion
And I can imagine that extra checks are performed during the compilation into MSIL. But what is executed by the CLR ? The code compiled by the JIT (or NGEN) from the MSIL.
Sorry if I insist on that point but let's take another example and the differences between Convert.ToInt32 and Conversions.ToInteger (introduced in .NET FX 2.0). In that case, the two methods are converted to something different in MSIL. So I do not have any problem to admit than the Conversions class is doing something else (even if I assume that it is doing a simple delegation to provide to the VB developpers the ability to work with VB types instead of CLR types)
To sum up, how can the same MSIL code (produced either by CType or DirectCast) be executed differently ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim I As Integer = CType(Q, Integer) ' Succeeds.
Dim J As Integer = DirectCast(Q, Integer) ' Fails.
Both keywords take an expression to be converted as the first argument, and the type to convert it to as
the second argument. Both conversions fail if there is no conversion defined between the data type of
the expression and the data type specified as the second argument.
The difference between the two keywords is that CType succeeds as long as there is a valid conversion
defined between the expression and the type, whereas DirectCast requires the run-time type of an object
variable to be the same as the specified type. If the specified type and the run-time type of the expression
are the same, however, the run-time performance of DirectCast is better than that of CType.
In the preceding example, the run-time type of Q is Double. CType succeeds because Double can be
converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer.