static void Main(string[] args) { // same as: MeDelegate del = new MeDelegate(Foo); MeDelegate del = Foo; InvokeTheDelegate(Foo); }
public IEnumerable <TArg> GetAllReturnValues <TArg>(MeDelegate <TArg> d) { foreach (MeDelegate <TArg> del in d.GetInvocationList()) { yield return(del()); } }
static void MainA() { MeDelegate d = Foo; d = (MeDelegate)Delegate.Combine(d, new MeDelegate(Goo)); d += Goo; d += Sue; d(); }
static IEnumerable <int> RunNumbers(IEnumerable <int> numbers, MeDelegate meDelegate) { foreach (var number in numbers) { if (meDelegate(number)) { yield return(number); } } }
static List <int> GetAllReturnValues(MeDelegate d) { List <int> ints = new List <int>(); foreach (MeDelegate del in d.GetInvocationList()) { ints.Add(del()); } return(ints); }
static IEnumerable <int> RunNumbersThruGauntlet(IEnumerable <int> numbers, MeDelegate gauntlet) { foreach (int number in numbers) { if (gauntlet(number)) { yield return(number); } } }
/// <summary> /// expects an impementation of bool MeDelegate(string s) /// and returns the names based on the delegate implementation. /// </summary> /// <param name="gauntlet"></param> /// <returns></returns> public IEnumerable <string> GetNamesOfLength3(MeDelegate gauntlet) { foreach (string name in _names) { if (gauntlet(name)) { yield return(name); } } }
/// <summary> /// Video tutorials on this class: /// 1. https://www.youtube.com/watch?v=UL2XHN6uJCg&list=PLAE7FECFFFCBE1A54 /// </summary> public void Run() { Foo(); MeDelegate del = new MeDelegate(Foo); // takes a method that returns void, del(); // del() is equivalent too: del.Invoke(); InvokeTheDelegat(del); //OR: InvokeTheDelegat(Goo); // equivalent: new MeDelegate(Goo) }
//Who is the delegate named gauntlet? //MeDelegate gauntlet = GreaterThanTen; //use it within a method or as obj. attribute //or externally: WhyDelegates.MeDelegate gauntlet = WhyDelegates.Function //The method using the delegate public static IEnumerable <int> ConsumeDelegate(IEnumerable <int> numbers, MeDelegate gauntlet) { foreach (int number in numbers) { if (gauntlet(number)) { yield return(number); } } }
public IEnumerable <string> GetNamesOfLength4() { _MyDelegate = OnlyOfLengthFour; //_MyDelegate = gauntlet; foreach (string name in _names) { if (_MyDelegate(name)) { yield return(name); } } }
public void Run() { MeDelegate <int> d = ReturnFive; //type we want... d += ReturnTen; d += Return22; foreach (int i in GetAllReturnValues <int>(d)) //type we want to use.. { Console.WriteLine(i); } }
static void Main(string[] args) { MeDelegate meDelegate = MyMetod; meDelegate(555); Program program = new Program(); meDelegate = program.MyNeStaticMetod; meDelegate(555); Console.ReadKey(); }
static void Main(string[] args) { MeDelegate d = ReturnFive; d += ReturnTen; d += Return22; foreach (int i in GetAllReturnValues(d)) { Console.WriteLine(i); } }
static void Main(string[] args) { MeDelegate d = Foo; Console.WriteLine(d.Method); Console.WriteLine(d.Target); Program m = new Program(); d = m.Goo; Console.WriteLine(d.Method); Console.WriteLine(d.Target); Console.ReadLine(); }
static void Main(string[] args) { // Inheritance chain: // Delegate -> MulticastDelegate -> MeDelegate MeDelegate d = ReturnFive; d += ReturnTen; d += ReturnTwelve; foreach (int i in GetAllReturnValues(d)) { Console.WriteLine(i); } }
public void Run() { MeDelegate d = Foo; //d(); //d = Goo; //d(); //d = Sue; //d(); //Chain these together: d += Goo; d += Sue; d += Foo; d(); }
static void Main(string[] args) { MeDelegate del = Call; del(1); Wrapper(x => Console.WriteLine(x)); //temp ------------------------------------------------- var testList = Test.SomeList.FilterList(x => x > 3); foreach (var l in testList) { Console.WriteLine(l); } }
static void Main(string[] args) { MeDelegate d = Foo; MeDelegate oldD = d; d = (MeDelegate)Delegate.Combine(d, new MeDelegate(Goo)); d += Sue; d += Foo; d -= Foo; foreach (MeDelegate m in d.GetInvocationList()) { Console.WriteLine(m.Target + ": " + d.Method); } d(); }
static void ChainingDelegates() { MeDelegate meDelegate = One; //Creates a delegates that points to "One" Method //Combining another delegate meDelegate += Two; //meDelegate = (MeDelegate)Delegate.Combine(meDelegate, new MeDelegate(Two)); meDelegate += Three; meDelegate += One; meDelegate(); //Removes the first "One" method from the end meDelegate -= One; }
static void Main(string[] args) { // Delegate keeps track of two Properties: // Method: gets the method represented by the delegate // Target: gets the class instance on which the current delegate invokes the instance method MeDelegate deleg1 = MyClass.Foo; Console.WriteLine(deleg1.Method); Console.WriteLine(deleg1.Target); // returns nothing because Foo is a static method MyClass m = new MyClass(); MeDelegate deleg2 = m.Goo; Console.WriteLine(deleg2.Method); Console.WriteLine(deleg2.Target); }
static void Main() { MeDelegate d = ReturnFive; d += ReturnTen; d += ReturnTwenty; int value = d(); List <int> ints = new List <int>(); foreach (MeDelegate del in d.GetInvocationList()) { ints.Add(del()); } foreach (int i in ints) { Console.WriteLine(i); } }
static void Main(string[] args) { MeDelegate d = Foo; // += and -= is the syntactic sugar // This is what's going on under the hood: // d = (MeDelegate)Delegate.Combine(d, new MeDelegate(Goo)); // Used mostly for events and the Observer Pattern d += Goo; d += Sue; d += Foo; d -= Foo; foreach (MeDelegate m in d.GetInvocationList()) { Console.WriteLine(m.Target + ": " + m.Method); } }
private void Start() { MeDelegate meDelegate = new MeDelegate(Goo); MeDelegate meDelegate1 = Goo; meDelegate(); meDelegate1(); // meDelegate.Invoke(); Debug.Log($"Target: {meDelegate.Target}, Mehthod: {meDelegate.Method.ToString()}"); var numbers = new List <int>() { 1, 2, 11, 55, 4 }; var result = GetAllTheNumbersLessThanCheck(10, numbers); var result1 = GetAllTheNumbersLessThanFive(numbers); var result2 = GetAllTheNumbersGeaterThanThirty(numbers); foreach (var i in result) { Debug.Log(i); } foreach (var i in result1) { Debug.Log(i); } foreach (var i in result2) { Debug.Log(i); } var result3 = RunNumbersThroughTheGauntlet(numbers, LessThanFive); // var result4 = RunNumbersThroughTheGauntlet(numbers, bool LessThanTen (int n){ return n < 10; }); var result4 = RunNumbersThroughTheGauntlet(numbers, n => n < 5); result3.ForEach(n => Debug.Log(n)); result4.ForEach(n => Debug.Log($"result4: {n}")); }
private void Awake() { MeDelegate meDelegate0 = Foo; meDelegate0 += Goo; meDelegate0 += Foo; meDelegate0 += Foo; meDelegate0 -= Foo; // meDelegate = (MeDelegate)Delegate.Combine(meDelegate, (MeDelegate)Goo); // meDelegate = (MeDelegate)Foo + Goo foreach (var del in meDelegate0.GetInvocationList()) { Debug.Log($"Target:{del.Target}, method:{del.Method}"); } Action <string> myAction = new Action <string>(MyActionMethod); Func <int, bool> myFunc = new Func <int, bool>(MyFuncMethod); myAction("Ahahaha"); Debug.Log(myFunc(4)); }
static void Main(string[] args) { MeDelegate d = Poo; Console.WriteLine(d.Target); Console.WriteLine(d.Method); //Output: // - No object //Void Poo(int32) - Shows invoking method and parameters Target target = new Target(); target.Hoo(7); d = target.Hoo; Console.WriteLine(d.Method); Console.WriteLine(d.Target); //Output: //Void Hoo(int32) - Shows invoking method and parameters //TargetDelegate - Shows targeted object ("d") class name ("Target") }
static void Main(string[] args) { MeDelegate del = M1; del += M2; del += M3; MeDelegate2 del2 = M1; del2 += M2; del2 += M3; int val = del(); Console.WriteLine(val); //Incase we need to print return value for each delegate invocation foreach (MeDelegate d in del.GetInvocationList()) { Console.WriteLine(d()); } }
static void Main(string[] args) { MeDelegate del = Foo; InvokeDelegate(del); var obj = new ProgramDelegate { Number1 = 10, Number2 = 5 }; OperationDelegate oDel = obj.AddOperation; //var sum = obj.InvokeOpDelegate(oDel); var sum = obj.InvokeOpDelegate((i, j) => { return(i + j); }); System.Console.WriteLine($"Sum is {sum}"); oDel = obj.SubtractOperation; //var res = obj.InvokeOpDelegate(oDel); var res = obj.InvokeOpDelegate((i, j) => { return(i - j); }); System.Console.WriteLine($"Subtraction result is {res}"); //sum = obj.InvokeOpDelegate() }
static void Main(string[] args) { MeDelegate Md = new MeDelegate(MyMethod); Md += MyMethod; Console.WriteLine("***********"); Console.WriteLine("Latest added method to Md delegate: " + Md.Method); Console.WriteLine("Target of Md delegate: " + Md.Target); Md += () => Console.WriteLine("I'm coming from lambda expression"); Console.WriteLine("After adding lambda: " + Md.Method); //ConsumeAndInvokeMethod(Md); //ConsumeAndInvokeMethod(new MeDelegate(() => Console.WriteLine("What am I doing here!?"))); //Md.Invoke(); //MeDelegateTakeStringReturnString Mdstr = (name) => { return "My name is:" + name; }; //Console.WriteLine(Mdstr("Don Juan")); MeDelegateTakeStringReturnString Mdstr = new Program().ReturnName; Console.WriteLine(Mdstr("Tomek")); Console.WriteLine("Small check for Mdstr - target: " + Mdstr.Target + " || method: " + Mdstr.Method); if (Mdstr.Method.ToString().Equals("System.String ReturnName(System.String)")) { Console.WriteLine("Oh my god, return type for Mdstr delegate is a string!"); } else { Console.WriteLine("I don't know what is it !"); } IEnumerable <int> NumbersBiggerThanTen = SelectedNumbers(new[] { 1, 2, 3, 10, 15, 25, 276, 326 }, new Program().VerifyIfNumberIsBiggerThan); Console.WriteLine("****************"); foreach (int number in NumbersBiggerThanTen) { Console.WriteLine("Number bigger than five: " + number); } Console.WriteLine("Invocation of SelectedNumbers method with using lambda method"); IEnumerable <int> NumbersBiggerThan200 = SelectedNumbers(new[] { 1, 2, 3, 10, 15, 25, 276, 326 }, (n) => n > 200); foreach (int number in NumbersBiggerThan200) { Console.WriteLine("Number bigger than two hundread: " + number); } Console.WriteLine("********************"); Console.WriteLine("************DELEGATE CHAINING***********"); MeDelegate delegateChain = () => Console.WriteLine("First method"); delegateChain += () => Console.WriteLine("Second method method in chain"); delegateChain += MyMethod; //Last delegate in chain returns value Console.WriteLine("Invoke chain"); Console.WriteLine("####################"); foreach (MeDelegate md in delegateChain.GetInvocationList()) { Console.WriteLine("Method: " + md.Method + " , target: " + md.Target); } Console.WriteLine("*****"); Console.WriteLine("Generic delegate in action");
static void ConsumeAndInvokeMethod(MeDelegate md) { md.Invoke(); }
static void InvokeTheDelegate(MeDelegate deleg) { // same as: deleg.Invoke(); deleg(); }