Skip to content

Instantly share code, notes, and snippets.

@ahmedtalaat327
Created March 7, 2022 01:06
Show Gist options
  • Select an option

  • Save ahmedtalaat327/0ab38f7f097951a5f674a0c2e0f55c47 to your computer and use it in GitHub Desktop.

Select an option

Save ahmedtalaat327/0ab38f7f097951a5f674a0c2e0f55c47 to your computer and use it in GitHub Desktop.
Explanation for delgation and lamda expression.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegatesTest
{
class Program
{
private delegate int SumPro(int f, int s);//=> Func<int f,int s, int r>
private delegate int incre(int s); //=> Func<int x,int r>
private delegate void prnt(int s); //=> Action<int x>
static void Main(string[] args)
{
//normal delegation
//with return type
SumPro delMethd = new SumPro(Sumition);
var result = delMethd(4, 5);
Console.WriteLine(result);
//no return
prnt delprntMethod = new prnt(log);
delprntMethod(455);
//anonys delegation
//with return type
SumPro anonyDelMethod = delegate (int x, int y) { return x + y; };
var result2 = anonyDelMethod(7, 3);
Console.WriteLine(result2);
//no return
prnt annonyprntMehod = delegate (int x) { Console.WriteLine(x); };
annonyprntMehod(477);
//lambda non pro way [easy expression instead of delegate]
//with return type
SumPro lmdaNonMethod = (p, j) => { return p + j; };
var result3 = lmdaNonMethod(6, 1);
Console.WriteLine(result3);
//non return
prnt lmdaNonprintMethod = (p) => { Console.WriteLine(p); };
lmdaNonprintMethod(88);
//lambda pro way [params only one and body only return or samll code]
//with return type
incre lmdaMethod = p => p+8;
// non return
prnt lmdaprntmethod = p => Console.WriteLine(p);
//--------------------------------------using predifined delgate [Action and Func]---------------
//Action to do more code without specifying a return [like non pro lmda but no return] <in,in,....,in>
Action<int> myAction = (x) => { var y = x + 1; Console.WriteLine(x++); };
myAction.Invoke(455000);
//Fun like Action but can return last args <in,in,...,out>
Func<int, int, int> myFunSum = (f, s) => s + f;
var resFun = myFunSum(4, 44);
//------------test on passing func
//return student name length that matches it's order in the list
var students = new List<string> { "Ahemd" , "Sara" , "ezz" , "Mohamed", "Sameh" , "Heba" };
var results = students.FindMyItemTextThatMatchOrderOfIt(student =>student.Length);
var postalcodes = new List<int> { 222,1158,20332,477,25608,2566,1470 };
var results2 = postalcodes.FindMyItemTextThatMatchOrderOfIt(code => code.ToString().Length);
Console.ReadLine();
}
private static int Sumition(int a,int b)
{
return a + b;
}
private static void log(int x)
{
Console.WriteLine(x);
}
}
public static class Helpers
{
public static List<int> FindMyItemTextThatMatchOrderOfIt<T>(this List<T> studenMenu,Func<T,int> finder)
{
var results = new List<int>();
for (int z = 0; z < studenMenu.Count; z++)
{
if (finder(studenMenu[z]) == z+1)
results.Add(z+1);
}
return results;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment