>>using System; >>using System.Collections.Generic; >>using System.Linq; >>using System.Text; >> >>namespace GregoryAdam.Test >>{ >> public class CaseTest >> { >> public static void Main() >> { >> int result = 0; >> int a = 23, b = 27; >> >> Case.Execute >> ( >> new CaseOne( a==1, () => { result = 1;}), >> new CaseOne( b==2, () => { result = 2;}), >> // otherwise >> new CaseOne( true, () => { result = 99;}) >> ); >> >> Console.WriteLine("result = {0}", result); >> Console.ReadLine(); >> } >> } >> >> public static class Case >> { >> public static void Execute(params CaseOne[] cases) >> { >> for( int i = -1; ++i < cases.Length; ) >> if (cases[i].Condition) >> { >> cases[i].Act(); >> break; >> } >> } >> } >> public class CaseOne >> { >> public bool Condition; >> public Action Act; >> >> public CaseOne(bool condition, Action act) >> { >> Condition = condition; >> Act = act; >> } >> } >> >>} >>>
delegate void Action() delegate void Action<T> (T arg) delegate void Action<T1, T2>(T1 arg1, T2, arg2) delegate void Action<T1, T2, T3>(T1 arg1, T2, arg2, T3 arg3) delegate void Action<T1, T2, T3, T4>(T1 arg1, T2, arg2, T3 arg3, T4 arg4)Closely related - they return a result - also with a max of 4 generic args
delegate TResult Func<TResult>() // http://msdn.microsoft.com/en-us/library/bb534960.aspx delegate TResult Func<T, TResult>( T arg) // http://msdn.microsoft.com/en-us/library/bb549151.aspx delegate TResult Func<T1, T2, TResult>( T1 arg1, T2 arg2) delegate TResult Func<T1, T2, T3, TResult>( T1 arg1, T2 arg2, T3 arg3) delegate TResult Func<T1, T2, T3, T4, TResult>( T1 arg1, T2 arg2, T3 arg3, T4 arg4)Happy 2009