8.08.2012

DOTNET Programming Concepts Difference FAQs-3

1.Difference between directCast and ctype in .NET
S.No
DirectCast
ctype
1
DirectCast is generally used to cast reference types.
Ctype is generally used to cast value types.
2
When you perform DirectCast on arguments that don't match then it will throw InvalidCastException.
Exceptions are not thrown while using ctype.
3
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.
4
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.
5
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.
6
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.

2.Difference between Convert.ToString() and object.ToString()
S.No
Convert.ToString()
object.ToString()
1
Convert.ToString(object) does not give any error in case of object value is null
object.ToString() give run time error if object value is null
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 not throw any error if any of strings value is null
If any of strings value is null, string1.Equals(string2) will throw 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 only .net compliance exceptions .
catch() will catch all types of 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 convert any value from any data type to another data type. This class consist of different methods for making conversions.
The Parse method is used to convert only one string value to any other data type. Every data type is consisting of parse() method.
2
Example:
1) char ch = Convert.ToChar("x"); -> string to character
2) float f = Convert.ToSingle(45); -> int to float
3) int x = Convert.ToInt(4.5f); -> float to int
Example:
1) int x = int.Parse("600"); -> string to int
2) char ch = char.Parse("dnf"); -> string to character

1 comment:

  1. This is very nice blog,and it is helps for student's.Thanks for info
    Dot Net Online course

    ReplyDelete