You are on page 1of 3

1.Difference between directCast and ctype in .NET S.

No 1 2 DirectCast ctype

DirectCast is generally used to cast Ctype is generally used to cast value reference types. types. When you perform DirectCast on Exceptions are not thrown while using arguments that don't match then it ctype. will throw InvalidCastException. 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. To perform DirectCast between two different classes, the classes should have a relationship between them. Performance of DirectCast is better than ctype. This is because no runtime helper routines of VB.NET are used for casting. 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 ctype between two different value types, no relationship between them is required. If the conversion is legal then it will be performed. 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 Ctype is specific to VB.NET and it is languages since it is not very not portable. specific to VB.NET

2.Difference between Convert.ToString() and object.ToString() S.No 1 Convert.ToString() object.ToString()

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

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 1 String.Equals(string1,string2) string1.Equals(string2)

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 1 catch(Exception objex) catch() block

catch(Exception objEx) will catch catch() will catch all types of exception only .net compliance exceptions . i.e., both non-compliance and .net compliance exceptions

5.Difference between "Convert" class and "Parse()" method S.No 1 Convert Class 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. 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); -> Parse() method The Parse method is used to convert only one string value to any other data type. Every data type is consisting of parse() method. Example: 1) int x = int.Parse("600"); -> string to int 2) char ch = char.Parse("dnf"); -> string to character

float to int And, further updates on difference between questions and answers, please visit my blog @ http://onlydifferencefaqs.blogspot.in/

You might also like