var item = "" + dr["someString"];
Or
var item = Convert.ToString(dr["somestring"]);
What are the performance implications of the above examples?
var item = "" + dr["someString"];
Or
var item = Convert.ToString(dr["somestring"]);
What are the performance implications of the above examples?
How about:
var item = (string)dr["someString"] ?? "";
which avoids an unnecessary concatenation, an unnecessary virtual call, and avoids the risk of calling a method on a null reference. It isn't clear what dr
is in this context; if it is a data-reader (IDataReader
etc) you might need:
int index = dr.GetOrdinal("someString");
var item = dr.IsDBNull(index) ? "" : dr.GetString(index);
or in the case of a DataTable
, something involving DataRow.IsNull
.
Whats wrong with dr["someString"].ValueOrEmpty()
?
As for your original question. The performance will not matter. I promise you that there are several other areas that can be optimized which gives a lot better performance gain.
Write the most readable code. And do not optimize until it's proven that you need to.
Update:
public static class StringExtensions
{
public static string ValueOrEmpty(this object obj)
{
return obj == null ? "" : obj.ToString();
}
}