Sometimes you just need a ternary statement, i.e. you don't want to return a value from the condition ? true : false construct. Here is an extension method to help you out (the usage example isn't the best).
class Program
{
static void Main(string[] args)
{
List<string> test = new List<string>();
ArrayList source = new ArrayList();
source.Add(1);
source.Add("hello");
source.Each<object>(x =>
(x is String).IfElse
(
() => test.Add((string)x),
() => test.Add(x.ToString())
));
}
}
static class Extensions
{
public static void If(this bool @this, Action isTrue)
{
if (@this)
isTrue();
}
public static void IfElse(this bool @this, Action isTrue, Action isFalse)
{
if (@this)
isTrue();
else
isFalse();
}
internal static void Each<T>(this IEnumerable<T> @this, Action<T> action)
{
foreach (T t in @this)
action(t);
}
internal static void Each<T>(this IEnumerable @this, Action<T> action)
{
foreach (object t in @this)
if (t is T)
{
action((T)t);
}
}
}