Skip to content

Instantly share code, notes, and snippets.

@cgbeutler
Last active April 12, 2019 01:33
Show Gist options
  • Save cgbeutler/2746af038ec3616e3d5c5859d9966396 to your computer and use it in GitHub Desktop.
Save cgbeutler/2746af038ec3616e3d5c5859d9966396 to your computer and use it in GitHub Desktop.
Functional if statements for C# that pass the calling object into the conditional function, then function, and else function.
// Use like so:
// Console.WriteLine("hello".If(s => s.length == 5, s => s + " world"));
// var newObj = someObj.If(null, new Foo(), o => o.bar());
// return response
// .If(r => r?.Content == null, "", r => r.Content.ReadAsStringAsync().Response)
// .If(string.IsNullOrWhiteSpace, "", content => $"----\n\t{content}\n----")
namespace FuncIf
{
public static class FuncIfExtensions
{
public static TResult If<TSource, TResult>(this TSource source, bool condition, TResult thenObj, TResult elseObj)
{
return condition ? thenObj : elseObj;
}
public static TResult If<TSource, TResult>(this TSource source, bool condition, Func<TSource, TResult> thenFunc, TResult elseObj)
{
if (thenFunc == null)
{
throw new ArgumentNullException(nameof(thenFunc));
}
return condition ? thenFunc(source) : elseObj;
}
public static TResult If<TSource, TResult>(this TSource source, bool condition, TResult thenObj, Func<TSource, TResult> elseFunc = null)
{
return condition
? thenObj
: elseFunc != null
? elseFunc(source)
: default(TResult);
}
public static TResult If<TSource, TResult>(this TSource source, bool condition, Func<TSource, TResult> thenFunc, Func<TSource, TResult> elseFunc = null)
{
if (thenFunc == null)
{
throw new ArgumentNullException(nameof(thenFunc));
}
return condition
? thenFunc(source)
: elseFunc != null
? elseFunc(source)
: default(TResult);
}
public static TResult If<TSource, TResult>(this TSource source, Func<TSource, bool> condition, TResult thenObj, TResult elseObj)
{
if (condition == null)
{
return source == null ? thenObj : elseObj;
}
return condition(source) ? thenObj : elseObj;
}
public static TResult If<TSource, TResult>(this TSource source, Func<TSource, bool> condition, Func<TSource, TResult> thenFunc, TResult elseObj)
{
if (thenFunc == null)
{
throw new ArgumentNullException(nameof(thenFunc));
}
if (condition == null)
{
return source == null ? thenFunc(source) : elseObj;
}
return condition(source) ? thenFunc(source) : elseObj;
}
public static TResult If<TSource, TResult>(this TSource source, Func<TSource, bool> condition, TResult thenObj, Func<TSource, TResult> elseFunc = null)
{
if (condition == null)
{
return source == null
? thenObj
: elseFunc != null
? elseFunc(source)
: default(TResult);
}
return condition(source)
? thenObj
: elseFunc != null
? elseFunc(source)
: default(TResult);
}
public static TResult If<TSource, TResult>(this TSource source, Func<TSource, bool> condition, Func<TSource, TResult> thenFunc, Func<TSource, TResult> elseFunc = null)
{
if (thenFunc == null)
{
throw new ArgumentNullException(nameof(thenFunc));
}
if (condition == null)
{
return source == null
? thenFunc(source)
: elseFunc != null
? elseFunc(source)
: default(TResult);
}
return condition(source)
? thenFunc(source)
: elseFunc != null
? elseFunc(source)
: default(TResult);
}
public static bool notNull<TValue>(TValue value)
{
return value != null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment