Reputation: 341
I don't really know how to phrase this well but I confused about performance
I have variables that read from the dictionary. but sometimes it can contain null.
For example, I have the following:
Dictionary<string,object> DataRow = new Dictionary<string,object>();
The previous line is reading Row from database Table and fetch it into Dictionary.
Now we can say we have columns: "StudentID : int
", "StudentName : varchar
", "IsPaid : bit
"
Sometimes Columns can be null. and I don't want to use nullable:
Now I try to do the following: First Approach
var StudentIDObject = DataRow["StudentID"];
var StudentID = StudentIDObject is null ? 1 : Convert.ToInt32(StudentIDObject);
var IsPaidObject = DataRow["IsPaid"];
var IsPaid = IsPaidObject is null ? true : Convert.ToBoolean(IsPaidObject); // note that sometimes null well be true depending on option behaviour.
I fell the previous code is ok and performance well be good. but it's not readability. depending I have more than 25 variables. I just give a simple example.
Why just don't use the following ? Second Approach
var StudentID = DataRow["StudentID"] is null ? 1 : Covnert.ToBoolean(DataRow["StudentID"]);
Note that the previous line will call the dictionary indexer two times. which I think searches in the dictionary again. is this leaks performance?
Should I use the first approach or second approach? and why?
Note that I have more than 25 variables. so that I try to figure out which is good performance while reading ? does it matter? ...
See Reference .NET source. Dictionary always doing loop when indexer called.
public TValue this[TKey key] {
get {
int i = FindEntry(key);
if (i >= 0) return entries[i].value;
ThrowHelper.ThrowKeyNotFoundException();
return default(TValue);
}
set {
Insert(key, value, false);
}
}
private int FindEntry(TKey key) {
if( key == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (buckets != null) {
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next) {
if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) return i;
}
}
return -1;
}
Upvotes: 0
Views: 46
Reputation: 11899
My opinion on the performance is, it doesn't matter. How fast C#, as compared to accessing a database, is basically irrelevant.
My opinion on the readability is this:
You should have a function that reads each type of value from a DataRow and converts it into the correct type:
var StudioId = ReadAsInt(DataRow["StudentId"]);
var IsPaid = ReadAsBoolean(DataRow["IsPaid"]);
Is much easier to read than your version. You will require those functions, of course, so something like:
int ReadAsInt(object value)
{
return value is null ? 1 : Convert.ToInt32(value);
}
int ReadAsBool(object value)
{
return value is null ? true : Convert.ToBoolean(value);
}
Or use Generic-Version
public static class Conversion
{
public static T Read<T>(object Value, T DefaultValue = default(T))
{
if (Value is null || Value is DBNull)
return DefaultValue;
return (T)Convert.ChangeType(Value, typeof(T));
}
}
Upvotes: 1