עליך להתחבר בכדי להוריד קבצים מהאתר
על מנת להבטיח שמשאבינו לא ינוצלו לרעה.
Predicate<T>, Action<T>, Func<T> |
|
public List<T> FindAll(Predicate<T> match)
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : List<int> list = new List<int> { 1, 43, 6, 12, 5, 23, 7, 86, 3, 49 };
6 : // C# 1.0 style
7 : List<int> greater_than_10 = list.FindAll(GreaterThanTen);
8 : foreach (int num in greater_than_10)
9 : {
10 : Console.Write(num + ",");
11 : }
12 : Console.WriteLine();
13 :
14 : // C# 2.0 style
15 : greater_than_10 = list.FindAll(delegate(int num) { return num > 10; });
16 : foreach (int num in greater_than_10)
17 : {
18 : Console.Write(num + ",");
19 : }
20 : Console.WriteLine();
21 : // C# 3.0 style
22 : greater_than_10 = list.FindAll(num => num > 10);
23 : foreach (int num in greater_than_10)
24 : {
25 : Console.Write(num + ",");
26 : }
27 : Console.WriteLine();
28 : }
29 : public static bool GreaterThanTen(int num)
30 : {
31 : return num > 10;
32 : }
33 : }
public delegate void Action<in T1, in T2>(T1 arg1,T2 arg2)
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : Action<int, int, int, int> sum = (n1, n2, n3, n4) => Console.WriteLine(n1 + n2 + n3 + n4);
6 : sum(11, 22, 33, 44);
7 :
8 : string message1 = "The first line of a message";
9 : string message2 = "The second line of a message";
10 : string message3 = "The third line of a message";
11 : Action<string, string, string> print = (s1, s2, s3) =>
12 : {
13 : Console.WriteLine("{0}", s1);
14 : Console.WriteLine("{0}", s2);
15 : Console.WriteLine("{0}", s3);
16 : };
17 : print(message1, message2, message3);
18 : }
19 : }
public void ForEach(Action<T> action)
ואיך מייצגים את הפעולה המסוימת? באמצעות Action:
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : List<String> names = new List<String>() { "Elimelech", "Zrubavel", "Havakuk", "Yerachmiel" };
6 : names.ForEach(str => Console.WriteLine(str));
7 :
8 : List<Robot> list = new List<Robot>
9 : {
10 : new Robot { Name = "Wall-E", Id = "DZX17767" },
11 : new Robot { Name = "Rodriguez", Id = "WRT12" },
12 : new Robot { Name = "HAL 9000", Id = "DHG167" },
13 : new Robot { Name="ASIMO", Id="TT76" },
14 : new Robot { Name="Rosie ", Id="HJTP95455" },
15 : new Robot { Name=" Optimus Prime ", Id="EKL3" }
16 : };
17 : list.ForEach(robot => Console.WriteLine("My name is {0}", robot.Name));
18 : }
19 : }
20 : class Robot
21 : {
22 : public string Name { get; set; }
23 : public string Id { get; set; }
24 : }
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2) |
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : Func<double, double> Square = x => x * x;
6 : var result1 = Square(5);
7 : Console.WriteLine(result1);
8 :
9 : Func<double, double, double> Mul = (x, y) => x * y;
10 : var result2 = Mul(5, 3);
11 : Console.WriteLine(result2);
12 :
13 : Func<double[], double[], double> SumArray = (x, y) =>
14 : {
15 : var sum = 0.0;
16 : x.ToList().ForEach(num => sum += num);
17 : y.ToList().ForEach(num => sum += num);
18 : return sum;
19 : };
20 : double[] arr1 = new double[] { 1.1, 2.2, 3.3, 4.4 };
21 : double[] arr2 = new double[] { 1.1, 2.2, 3.3 };
22 : var result3 = SumArray(arr1, arr2);
23 : Console.WriteLine(result3);
24 : }
25 : }
1 : public static void Method1()
2 : {
3 : string[] friends_name = { "Elimelech", "Zrubavel", "Havakuk", "Yerachmiel" };
4 : double average = friends_name.Average(s => s.Length);
5 : Console.WriteLine("The average string length is {0}.", average);
6 : }
1 : public static int MyFunc(string s)
2 : {
3 : return s.Length;
4 : }
5 : private static void Method2()
6 : {
7 : string[] friends_name = { "Elimelech", "Zrubavel", "Havakuk", "Yerachmiel" };
8 : Func<string, int> func = MyFunc;
9 : double average = friends_name.Average(func);
10 : Console.WriteLine("The average string length is {0}.", average);
11 : }
1 : private static void Method3()
2 : {
3 : List<String> names = new List<String>() { "Zelda", "Zrubavel", "Elimelech", "Havakuk", "Yerachmiel", "Yona", "Zalman", "Pirhiya", "Fruma" };
4 : var tmp1 = names.OrderBy(str => str).ToList();
5 : tmp1.ForEach(str => Console.WriteLine(str));
6 : }
1 : private static void Method4()
2 : {
3 : List<String> names = new List<String>() { "Zelda", "Zrubavel", "Elimelech", "Havakuk", "Yerachmiel", "Yona", "Zalman", "Pirhiya", "Fruma" };
4 : var tmp2 = names.OrderBy(str => str.Length).ToList();
5 : tmp2.ForEach(str => Console.WriteLine(str));
6 : }
1 : private static void Method4()
2 : {
3 : List<String> names = new List<String>() { "Zelda", "Zrubavel", "Elimelech", "Havakuk", "Yerachmiel", "Yona", "Zalman", "Pirhiya", "Fruma" };
4 : var tmp1 = names.OrderByDescending(str => str).ToList();
5 : tmp1.ForEach(str => Console.WriteLine(str));
6 : }
1 : private static void Method5()
2 : {
3 : List<String> names = new List<String>() { "Zelda", "Zrubavel", "Elimelech", "Havakuk", "Yerachmiel", "Yona", "Zalman", "Pirhiya", "Fruma" };
4 : var tmp3 = names.Where(str => str.Length > 6).ToList();
5 : tmp3.ForEach(str => Console.WriteLine(str));
6 : }
1 : private static void Method6()
2 : {
3 : List<String> names = new List<String>() { "Zelda", "Zrubavel", "Elimelech", "Havakuk", "Yerachmiel", "Yona", "Zalman", "Pirhiya", "Fruma" };
4 : var tmp1 = names.GroupBy(str => str[0]).ToList();
5 : tmp1.ForEach(arr =>
6 : {
7 : Console.WriteLine("Count = {0}", arr.Count());
8 : arr.ToList().ForEach(str => Console.WriteLine(str));
9 : Console.WriteLine("***************");
10 : });
11 : }