-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (129 loc) · 4.96 KB
/
Program.cs
File metadata and controls
146 lines (129 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
namespace DelegateTest
{
delegate bool PostCondition(Post post);
delegate void ActionInPost(Post post);
static class Program
{
private static void AddPostInMailBody(Post post)
{
throw new NotImplementedException();
}
private static bool PostIsMovieCategory(Post post)
{
return post.Equals("Movie");
}
private static void ExecActionInFilteredListOfPost(IEnumerable<Post> posts, PostCondition AnyCondition, ActionInPost AnyAction)
{
foreach (Post post in posts)
{
if (AnyCondition(post))
AnyAction(post);
}
}
private static void ExecAction(IEnumerable<Post> posts, Func<Post,bool> AnyCondition, Action<Post> AnyAction)
{
foreach (Post post in posts)
{
if (AnyCondition(post))
AnyAction(post);
}
}
static void Main(string[] args)
{
var posts = new List<Post>
{
new Post
{
Title = "Harry Potter I",
Summary = "The Philosopher's Stone ",
Category = "Movie"
},
new Post
{
Title = "Harry Potter II",
Summary = "Secret Chamber",
Category = "Movie"
},
new Post
{
Title = "Harry Potter III",
Summary = "The Prisoner of Azkaban",
Category = "Movie"
},
new Post
{
Title = "Game of Thrones",
Summary = "Winter is Coming",
Category = "Series"
},
new Post
{
Title = "10 Tips for Starting a Programming Career",
Summary = "Career Orientation",
Category = "Tips"
},
new Post
{
Title = "Refactoring",
Summary = "Improving design of existing code",
Category = "Books"
},
};
foreach (Post post in posts)
{
Console.WriteLine(post.Title);
}
foreach (Post post in posts)
{
if (post.Category.Equals("Movie"))
Console.WriteLine(post.Title);
}
foreach (Post post in posts)
{
if (post.Category.Equals("Movie"))
AddPostInMailBody(post);
}
foreach (Post post in posts)
{
if (PostIsMovieCategory(post))
Console.WriteLine(post.Title);
}
foreach (Post post in posts)
{
if (PostIsMovieCategory(post))
AddPostInMailBody(post);
}
//Método passando Métodos como parametros: MindBlow Nv:1!
ExecActionInFilteredListOfPost(posts, PostIsMovieCategory, AddPostInMailBody);
//Método passando delegates como parametros para executar uma operação (Obs.: sem precisar criar um Métodos): MindBlow Nv2!
ExecActionInFilteredListOfPost(
posts,
delegate (Post post) { return post.Category.Equals("Movie"); },
delegate (Post post) { Console.WriteLine(post.Title); }
);
//Método passando delegates como parametros para executar uma operação dessa vez com Lambda Expression: MindBlow Nv3!
ExecActionInFilteredListOfPost(
posts,
post => post.Category.Equals("Movie"),
post => Console.WriteLine(post.Title)
);
//USING NATIVE DELEGATE .NET
//Método passando Métodos como parametros: MindBlow Nv:1!
ExecAction(posts, PostIsMovieCategory, AddPostInMailBody);
//Método passando delegates como parametros para executar uma operação (Obs.: sem precisar criar um Métodos): MindBlow Nv2!
ExecAction(
posts,
delegate (Post post) { return post.Category.Equals("Filmes"); },
delegate (Post post) { Console.WriteLine(post.Title); }
);
//Método passando delegates como parametros para executar uma operação dessa vez com Lambda Expression: MindBlow Nv3!
ExecAction(
posts,
post => post.Category.Equals("Filmes"),
post => Console.WriteLine(post.Title)
);
}
}
}