Created
June 3, 2018 12:13
-
-
Save NaserKhoshfetrat/528a5fb84eafc32d681dc49f842e0581 to your computer and use it in GitHub Desktop.
c# get first or default value in generic method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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