To add to the above explanations,
another point which is also as important (if not more) than efficiency it is how “safe” your calls are against crashes and exceptions. We all know our users use our programs exactly as designed and NEVER try anything crazy… right?
When you do
(string)row["col"]
You are doing a cast you are telling the compiler that the object stored at row[“col”] is a string and you as the developer are guaranteeing this 100%. This is fine as long as a) that object is in fact a string and b) there is actually an object there. If you do the above on a null object or an integer you will get a runtime exception and your program will crash. That is because casting puts the onus on you to ensure that the data there is both valid and it exists.
Using the Convert.ToString() method avoids this, in the Convert.ToString() method the specification handles nulls gracefully and thus if you try to convert a null it will not crash your application,it just returns an Empty string. Further more it knows how to convert any primitive (int, decimal, long, double, date etc) into a string so it won’t crash like a cast would if the type of the object insn’t already a string. So although it does a bit more work and it is “less efficient” it also protects you from runtime errors and crashes.
Now using the ToString() method is a little different than casting and Convert.ToString() when you say .ToString() you are asking the interpreter to give you the string representation of the existing object. So it carries the same error prone behavior as a cast where by if the object is null it will throw a null object exception. However the difference between the cast and it is that the ToString() method can be overridden in your own classes. Take this class for example
public class Car
{
string Make {get;set;}
string Model {get;set;}
public Car(string make, string model)
{
this.Make = make;
this.Model = model;
}
public override string ToString()
{
return string.Format("This is a Car made by: {0} and the Model of the Car is: {1}", this.Make, this.Model);
}
}
So now if anyone uses the Car class and does a ToString() on it they will get the “String” representation of what a Car is as demonstrated in this snippet
That is to say that ToString() will not always return the “String” you expect, and again it is error prone if the object is null.
So although I agree that you should do what works for you, you should strive for error free code as much as possible, so when you are able to I would recommend using the Convert.ToString() method unless you are 100% sure that your object will ALWAYS exist and or that it is in fact a string.
You can obviously avoid these pitfalls by checking for null yourself or by using the null conditional operator.
var x = myObject?.ToString()
Which tells the compiler only do the stuff after the ? If the object isn’t null.
As stated above there are a thousand ways to skin a cat. I’m sure this was way more than you wanted to know as it relates to your original snippet.