Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(string)reader[0] vs Convert.ToString(reader[0])

what's better

var s =  (string)reader[0]  

or

var s = Convert.ToString(reader[0])

?

like image 394
Omu Avatar asked Feb 15 '10 14:02

Omu


People also ask

What is the difference between convert to string and ToString?

Here both the methods are used to convert the string but the basic difference between them is: "Convert" function handles NULLS, while "i. ToString()" does not it will throw a NULL reference exception error. So as good coding practice using "convert" is always safe.

How do I convert ToString?

Convert the specified value to its equivalent string using the ToString() method. Initialize a bool value. bool boolVal = false; Now, to convert it to a string, use the ToString() method.

What is the use of ToString () in C#?

ToString is the major formatting method in the . NET Framework. It converts an object to its string representation so that it is suitable for display.

Does ToString work on null C#?

.ToString()It will not handle NULL values; it will throw a NULL reference exception error.


1 Answers

I'd say reader.GetString(0)

like image 199
Joel Coehoorn Avatar answered Oct 15 '22 19:10

Joel Coehoorn