Hi Guest

MobileUser

1 HomePage 1 Jobs 1 WalkIn 1Articles  

Cat Exam

 :) Latest Topic
AJAX
VB.NET
SQL Query
UDDI
CLASS
JavaScript Questions
SharePoint Interview
SilverLight
WCF
general knowledge
 ASP.NET
 :) Hot Jobs
 
 :) Latest Articles


  

 


Main difference between DirectCast and CType?
Question Posted on 19 Nov 2021

Home >> DotNet >> ASP.NET >> Main difference between DirectCast and CType?




Main difference between DirectCast and CType?
Below are the main difference between DirectCast and CType
0
0

Base of differenceDirectCastCType
TypesWe generally used Directcast to cast reference types.We generally used Ctype to cast value types.
PortabilityDirectCast is portable across many languages as it is not very specific to VB.NET.On the other hand Ctype is specific to VB.NET and it is not portable.
PerformancePerformance of DirectCast is far better than ctype.Performance wise ctype is slow when compared to DirectCast.
RoutinesAs performance is better because no runtime helper routines of VB.NET are used for casting.As we are aware performamce is slower this is because ctype casting requires execution of runtime helper routines of VB.NET.
ExceptionIf we perform DirectCast on arguments if that don't match then it will throw InvalidCastException.Here exceptions are not thrown while using ctype.
ConversionIf we use DirectCast we cannot convert object of one type into another. Type of the object at runtime should be same as the type that is specified in DirectCast. Below is the example:- Dim sampleNumber as Integer Dim sampleString as String sampleNumber=400 sampleString=DirectCast(sampleNumber,String) Here above code will not work because runtime type of sampleNumber is Integer which is different from the specified type String.In Ctype cast of object of one type into another if the conversion is valid. Below is the example:- Dim sampleNumber as Integer Dim sampleString as String sampleNumber=400 sampleString=CType(sampleNumber,String) This code is legal and the Integer 100 is now converted to a string.


 
 Input Your Comments Or suggestions(If Any)
 Name
 Email(optional)
 Comments


Other Important Questions
Use of Calendar control in dotnet with code
How to improve Front End UI performance of dotnet web application?
File upload in dotnet
reverse a string
function of garbage collector






 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |