What is the Difference between directcast and ctype?



What is the Difference between directcast and ctype? ..

Answer / guest

Ans: The DirectCast keyword introduces a type conversion
operation. You use it the same way you use the CType
keyword, as the following example shows:
Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
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.
DirectCast throws an InvalidCastException error if the
argument types do not match.

Is This Answer Correct ?    4 Yes 4 No

Post New Answer

More C Sharp Interview Questions

What is the usage of transponders?

0 Answers   Wipro,


What are Indexers in C#?

0 Answers   SwanSoft Technologies,


How do I run a cshtml file?

0 Answers  


What is lazy loading entity framework?

0 Answers  


Explain about accessibility modifier 'protected internal'?

0 Answers  






How do you determine whether a string represents a numeric value?

0 Answers  


How do you prevent a method from being overridden in c#?

0 Answers  


What's the difference between class and object?

0 Answers  


How?s method overriding different from overloading?

3 Answers  


Is list a collection c#?

0 Answers  


What are the types of serialization?

0 Answers  


Method1() { int a=0, b=0, c=0; Monitor.Enter(this) c = 12; b = 3; a = c/b Moniter.Exit(this) } Method1() { int a=0, b=0, c=0; c = 12; b = 3; lock(this){ a = c/b } } Choose correct answer. a. Upon completion, Method1 and Method2 release the lock b. Upon Comletion, Method1 release the lcok and Method2 not. c. Upon Completion, Method2 release the lock and Method1 not. d. Upon Completion, neither Method1 or Method to release the lock.

1 Answers  


Categories