This document summarizes the key differences between various .NET concepts:
1. DirectCast casts reference types while ctype casts value types. DirectCast throws exceptions if types don't match while ctype does not. DirectCast requires matching types at runtime.
2. Convert.ToString allows null values while object.ToString causes errors for nulls.
3. String.Equals doesn't throw for nulls while string1.Equals does.
4. catch(Exception) handles all exceptions while catch() only handles .NET exceptions.
5. Convert class converts any type while Parse methods only convert strings to other types.
DirectCast vs CType in .NET - Key Differences Explained
1. 1.Difference between directCast and ctype in .NET
S.No DirectCast ctype
1 DirectCast is generally used to Ctype is generally used to cast value
cast reference types. types.
2 When you perform DirectCast on Exceptions are not thrown while using
arguments that don't match then it ctype.
will throw InvalidCastException.
3 If you use DirectCast, you cannot Ctype can cast object of one type into
convert object of one type into another if the conversion is valid.
another. Type of the object at Consider the following example:
runtime should be same as the Dim sampleNum as Integer
type that is specified in Dim sampleString as String
DirectCast. Consider the sampleNum = 100
following example: sampleString = CType(sampleNum,
Dim sampleNum as Integer String)
Dim sampleString as String This code is legal and the Integer 100
sampleNum = 100 is now converted to a string.
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.
4 To perform DirectCast between To perform ctype between two
two different classes, the classes different value types, no relationship
should have a relationship between them is required. If the
between them. conversion is legal then it will be
performed.
5 Performance of DirectCast is Performance wise, ctype is slow when
better than ctype. This is because compared to DirectCast. This is
no runtime helper routines of because ctype casting requires
VB.NET are used for casting. execution of runtime helper routines of
VB.NET.
6 DirectCast is portable across Ctype is specific to VB.NET and it is
many languages since it is not not portable.
very specific to VB.NET
2.Difference between Convert.ToString() and object.ToString()
S.No Convert.ToString() object.ToString()
1 Convert.ToString(object) does not object.ToString() give run time error if
give any error in case of object object value is null
value is null
2. Example:
object obj=null;
string str=obj.ToTsirng(); //it will give run time error
string str1=Convert.ToString(obj); // it will run, not give any error
3.Difference between String.Equals(string1,string2) and string1.Equals(string2)
S.No String.Equals(string1,string2) string1.Equals(string2)
1 String.Equals(string1,string2) will If any of strings value is null,
not throw any error if any of string1.Equals(string2) will throw
strings value is null runtime error
Example:
try
{
string str1 = null;
string str2 = "abc";
bool chk;
chk = str1.Equals(str2); // will give runtime error
chk = String.Equals(str1, str2); //will not give any error
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
4.Difference between catch(Exception objex) and catch() block
S.No catch(Exception objex) catch() block
1 catch(Exception objEx) will catch catch() will catch all types of
only .net compliance exceptions . exception i.e., both non-compliance
and .net compliance exceptions
5.Difference between "Convert" class and "Parse()" method
S.No Convert Class Parse() method
1 The Convert class is used to The Parse method is used to convert
convert any value from any data only one string value to any other data
type to another data type. This type. Every data type is consisting of
class consist of different methods parse() method.
for making conversions.
2 Example: Example:
1) char ch = Convert.ToChar("x"); 1) int x = int.Parse("600"); -> string to
-> string to character int
2) float f = Convert.ToSingle(45); 2) char ch = char.Parse("dnf"); ->
3. -> int to float string to character
3) int x = Convert.ToInt(4.5f); ->
float to int
And, further updates on difference between questions and answers, please visit my blog @
http://onlydifferencefaqs.blogspot.in/