What is the difference between directCast and ctype in .NET?

Difference between directCast and ctype in .NET

DirectCast
ctype
DirectCast is generally used to cast reference types. Ctype is generally used to cast value types.
When you perform DirectCast on arguments that don't match then it will throw InvalidCastException. Exceptions are not thrown while using ctype.
If you use DirectCast, you 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. Consider the following example:
Dim sampleNum as Integer
Dim sampleString as String
sampleNum = 100
sampleString = DirectCast(sampleNum, String)
This code will not work because the runtime type of sampleNum is Integer, which is different from the specified type String.
Ctype can cast object of one type into another if the conversion is valid. Consider the following example:
Dim sampleNum as Integer
Dim sampleString as String
sampleNum = 100
sampleString = CType(sampleNum, String)
This code is legal and the Integer 100 is now converted to a string.
To perform DirectCast between two different classes, the classes should have a relationship between them. To perform ctype between two different value types, no relationship between them is required. If the conversion is legal then it will be performed.
Performance of DirectCast is better than ctype. This is because no runtime helper routines of VB.NET are used for casting. Performance wise, ctype is slow when compared to DirectCast. This is because ctype casting requires execution of runtime helper routines of VB.NET.
DirectCast is portable across many languages since it is not very specific to VB.NET Ctype is specific to VB.NET and it is not portable.

| Can you call a constructor from another constructor of the Class in .NET? | Difference between Response.Output.Write() method and Response.Write() method in .NET | How do you establish multiple inheritance in C#? | How do you introduce a ReadOnly property in C#? | How do you perform constructor overloading in C#? | Is catch(Exception) recommended to be used in .NET? | What are the different access modifiers available in C#? | What are the different ways of overloading in C#? | What are the members of stringbuilder class in C#? | What is Multicast Delegate? Explain it with example in C# | What is the difference between abstract class and interface in .NET? | What is the difference between Clone and CopyTo methods in .NET | What is the difference between const and readonly in .NET | What is the difference between directcast and ctype in .NET? | What is the difference between out and ref parameters in .NET | What is the difference between public assembly and private assembly in .NET | What is the difference between strong typing and weak typing in .NET? | What is the difference between Trace and Debug in .NET | What is the need for Abstract Factory Pattern in C#? | What is the need for Adapter Pattern in C# |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.