Skip to content

Instantly share code, notes, and snippets.

@NaserKhoshfetrat
Created June 3, 2018 12:13
Show Gist options
  • Save NaserKhoshfetrat/528a5fb84eafc32d681dc49f842e0581 to your computer and use it in GitHub Desktop.
Save NaserKhoshfetrat/528a5fb84eafc32d681dc49f842e0581 to your computer and use it in GitHub Desktop.
c# get first or default value in generic method
public T GET_FIRST_OR_DEFAULT<T>(DataSet dataset)
{
return GET_FIRST_OR_DEFAULT<T>(dataset, default(T));
}
public T GET_FIRST_OR_DEFAULT<T>(DataSet dataset, T defaultValue)
{
bool status = true;
if (dataset.Tables.Count == 0)
{
status = false;
}
if (status && dataset.Tables[0].Rows.Count == 0)
{
status = false;
}
if (status)
{
return (T)Convert.ChangeType(
dataset.Tables[0].Rows[0][0].ToString()
, typeof(T));
}
else
{
return defaultValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment