Skip to content

Instantly share code, notes, and snippets.

@draevin
Last active May 30, 2018 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draevin/3b993ba36ca4de79e915431b35612530 to your computer and use it in GitHub Desktop.
Save draevin/3b993ba36ca4de79e915431b35612530 to your computer and use it in GitHub Desktop.
Running Collection of Extension Methods
//yeah yeah this isn't a fully formatted .cs file I know
//just put these somewhere, probably with the following:
//using System;
//using System.Linq;
//following datetime exts are good for output from db results, esp. for reporting
public static string ToStringOrNA(this DateTime? dt)
{
if (dt.HasValue)
{
return dt.Value.ToString();
}
else
{
return "N/A";
}
}
public static string ToStringOrDefaultValue(this DateTime? dt)
{
if (dt.HasValue)
{
return dt.Value.ToString();
}
else
{
return "N/A";
}
}
public static string ToShortDateStringOrNA(this DateTime? dt)
{
if (dt.HasValue)
{
return dt.Value.ToShortDateString();
}
else
{
return "N/A";
}
}
public static string ToShortDateStringOrDefault(this DateTime? dt, string defaultValue)
{
if (dt.HasValue)
{
return dt.Value.ToshortDateString();
}
else
{
return defaultValue;
}
}
//a few more mostly for output of DB data
//nullDefault is usually "N/A" or "Unknown" or smtn...
public static string BoolToYN(this bool? value, string nullDefault)
{
string result;
if (value == null)
{
result = nullDefault;
}
else
{
result = ((bool)value).BoolToYN();
}
return result;
}
public static string BoolToYN(this bool value)
{
return value ? "Yes" : "No";
}
//quick checks of contents
//this is where we use LINQ
public static bool IsOneOf<T>(this T needle, params T[] haystack)
{
return haystack.Contains(needle);
}
public static bool IsNoneOf<T>(this T needle, params T[] haystack)
{
return !haystack.Contains(needle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment