C# (Sharp) Programming Language Question:
Download Questions PDF

Difference between directcast and ctype.

C# (Sharp) Programming Language Interview Question
C# (Sharp) Programming Language Interview Question

Answer:

Anser1
DirectCast requires the run-time type of an object variable to bethe same as the specified type.The run-time performance ofDirectCast is better than that of CType, if the specified type and the run-time typeof the expression are the same. Ctype works fine if there is a valid conversion defined between the expression and the type.

Answer2
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.

Download C# (Sharp) Programming Language Interview Questions And Answers PDF

Previous QuestionNext Question
How do you convert a value-type to a reference-type?An example of a ctype and directcast.