-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.html
More file actions
293 lines (293 loc) · 10.4 KB
/
day3.html
File metadata and controls
293 lines (293 loc) · 10.4 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE HTML>
<html lang="de">
<head>
<title>Programming C#</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=1274, user-scalable=no">
<link rel="stylesheet" href="themes/styles/style.css">
</head>
<body class="list">
<header class="caption">
<h1>Programming C#</h1>
</header>
<div class="slide cover" id="Cover"><div>
<section>
<header>
<h2>Programming C#</h2>
<h3>Day 3: Lambda expressions, Delegates, extension methods, LINQ and debugging</h3>
</header>
<img src="pictures/cover.jpg" alt="">
</section>
</div></div>
<div class="slide" id="overview"><div>
<section>
<header>
<h2>Content</h2>
</header>
<ul>
<li>A simple Windows Forms project</li>
<!--<li>An overview of available controls</li>-->
<li>Building own user controls</li>
<li>Anonymous methods with lambda expressions</li>
<li>What is a delegate?</li>
<li>Anonymous objects</li>
<li>Extension methods</li>
<li>Example of extension methods: LINQ</li>
</ul>
</section>
</div></div>
<div class="slide" id="winforms-project"><div>
<section>
<header>
<h2>A simple WinForms project</h2>
</header>
<ul>
<li>Let's create a simple calculator!</li>
<li>What is needed?</li>
<li>How should the buttons work?</li>
<li>Using the designer for creating events</li>
<li>Understanding partial classes</li>
<li>Advanced topic: Separation of concerns</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-calculator"><div>
<section>
<header>
<h2 class="example">→ Example - A simple calculator</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day3/PocketCalc" title="Download source" class="example-download">PocketCalc.zip</a>
</section>
</div></div>
<div class="slide" id="winforms-controls"><div>
<section>
<header>
<h2>Controls in Windows Forms</h2>
</header>
<ul>
<li>All common Windows controls exist</li>
<li>Some new ones have been integrated as well</li>
<li><code>ComboBox</code>, <code>TextBox</code>, <code>Label</code>, <code>Button</code></li>
<li>Or with children: <code>TabControl</code>, <code>Panel</code>, <code>GroupBox</code></li>
<li>Specialized input: <code>NumericUpDown</code>, <code>DateTimePicker</code></li>
<li>Each control has a big range of (mostly useful) properties</li>
</ul>
</section>
</div></div>
<div class="slide" id="custom-controls"><div>
<section>
<header>
<h2>Creating custom controls</h2>
</header>
<ul>
<li>Most basic starting point: Inherit from <code>Component</code></li>
<li>Far better: Derive from <code>Control</code></li>
<li>Option with designer: <code>UserControl</code></li>
<li>Or pick one like <code>ScrollableControl</code></li>
<li>In most cases we have to do the drawing</li>
<li>Drawing is done over GDI+, more about it later</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-custom-control"><div>
<section>
<header>
<h2 class="example">→ Example - Create a control</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day4/BlinkLabel" title="Download source" class="example-download">BlinkLabel.zip</a>
</section>
</div></div>
<div class="slide" id="lambda-expessions"><div>
<section>
<header>
<h2>Anonymous methods</h2>
</header>
<ul>
<li>An anonymous method is one that has no name</li>
<li>In C# there is an easy way of creating those, using <code>=></code></li>
<li>This is the so called fat arrow operator</li>
<li>Example: <code>(x, y) => x * x + y</code></li>
<li>Or: <code>x => { return x > 0 ? 1 : (x < 0 ? -1 : 0); }</code></li>
<li>Omit round brackets is possible if just 1 argument</li>
<li>Curly brackets (and <code>return</code>) can be omitted if single statement</li>
</ul>
</section>
</div></div>
<div class="slide" id="delegates"><div>
<section>
<header>
<h2>Delegates</h2>
</header>
<ul>
<li>How to use anonymous methods if they have no name?</li>
<li>A pointer to the function is required</li>
<li><code>delegate [RET] [NAME]([ARGUMENTS])</code> solves this problem</li>
<li>This is like a managed function pointer</li>
<li>There are already some generic delegates like <code>Func<TReturn></code></li>
<li>Use: <code>Func<double, double> squ = x => x * x;</code></li>
<li>They can be used like functions, e.g. <code>squ(2)</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="example-lambda-expression"><div>
<section>
<header>
<h2 class="example">→ Example - Lambda expressions</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day3/Lambda.cs" title="Download source" class="example-download">Lambda.cs</a>
</section>
</div></div>
<div class="slide" id="anonymous-objects"><div>
<section>
<header>
<h2>Anonymous objects</h2>
</header>
<ul>
<li>C# lets us also create anonymous objects</li>
<li>Here the type is unknown, it will be compiler generated</li>
<li><code>new { Name = "Florian", Age = 28 }</code> creates such an object</li>
<li>Usage: (temporary) data-encapsulation</li>
<li>Advantage: No need to write a whole class for it</li>
<li>Disadvantage: Access to members only in local method</li>
<li>Passing such an object is only possible as <code>object</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="type-inference"><div>
<section>
<header>
<h2>Inferring types (<code>var</code>)</h2>
</header>
<ul>
<li>Problem: What's the type of the anonymous object?</li>
<li>We do not know, it is compiler generated...</li>
<li>Therefore can only use <code>object</code>, unless ...</li>
<li>... the compiler can detect the type! Keyword: <code>var</code></li>
<li>Simple: <code>var i = 4</code> will infer <code>Int32</code></li>
<li>Or something more complicated: <code>var pi = 3.14f</code> (<code>Single</code>)</li>
<li>Our case: <code>var a = new { Name = "My name" }</code> (who cares!)</li>
</ul>
</section>
</div></div>
<div class="slide" id="extension-methods"><div>
<section>
<header>
<h2>Extension methods</h2>
</header>
<ul>
<li>Scenario: We get a library with cool classes, but ...</li>
<li>... we see that a some useful methods are missing!</li>
<li>Usual solution: Write your own (<code>static</code>) methods</li>
<li>The first argument would always be an instance of the specific class</li>
<li>So why not bring together what belongs together?</li>
<li>Extension methods solve this problem with the <code>this</code> keyword</li>
<li>Before we had to write something like <code>MyClass.MyMethod(a)</code></li>
<li>Now we can just write <code>a.MyMethod()</code> (shorter and more expressive)</li>
</ul>
</section>
</div></div>
<div class="slide" id="extension-methods-recipe"><div>
<section>
<header>
<h2>Recipe: Extension methods</h2>
</header>
<pre>
<code>namespace <mark>MyExtensionMethods</mark></code>
<code>{</code>
<code> <mark>static</mark> class ExtMethods {</code>
<code> <mark>public static</mark> void Dump(<mark>this</mark> string s) {</code>
<code> Console.WriteLine(s);</code>
<code> }</code>
<code> }</code>
<code>}</code>
</pre>
</section>
</div></div>
<div class="slide" id="example-extension-methods"><div>
<section>
<header>
<h2 class="example">→ Example - Extension methods</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day3/Extensions.cs" title="Download source" class="example-download">Extensions.cs</a>
</section>
</div></div>
<div class="slide" id="linq"><div>
<section>
<header>
<h2>Language Integrated Query (LINQ)</h2>
</header>
<ul>
<li>A set of really useful extension methods</li>
<li>The required namespace is <code>System.Linq</code></li>
<li>Every <code>IEnumerable<T></code> has LINQ methods</li>
<li>Purpose: Run queries of (large) datasets</li>
<li>Advantage: Reduction of code</li>
<li>Highly useful: lambda expressions, anonymous objects</li>
<li>Sort some array e.g. <code>double[] a</code>? <code>a.OrderBy(m => m)</code></li>
<li>Get even elements of <code>int[] a</code>: <code>a.Where(m => m % 2 == 0)</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="venn-linq"><div>
<section>
<header>
<h2>LINQ hierarchy</h2>
</header>
<img src="pictures/linq.png" class="middle r" />
</section>
</div></div>
<div class="slide" id="linq-warning"><div>
<section>
<header>
<h2>Important facts about LINQ</h2>
</header>
<ul>
<li><code>foreach</code> is more expensive than <code>for</code></li>
<li>There is some overhead associated with LINQ statements</li>
<li>LINQ statements are deferred, i.e. only performed if requested</li>
<li>If your dataset changes, the LINQ result will change as well</li>
<li><code>IEnumerable<T></code> is in-memory query</li>
<li><code>IQueryable<T></code> is for remote-data</li>
<li>Important difference that does not matter for us (no remote data)</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-linq"><div>
<section>
<header>
<h2 class="example">→ Example - LINQ</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day3/Linq.cs" title="Download source" class="example-download">Linq.cs</a>
</section>
</div></div>
<div class="slide" id="presentations"><div>
<section>
<header>
<h2>All available presentations</h2>
</header>
<div class="left">
<b>Week 1</b>
<ul>
<li><a href="day1.html">Presentation of Monday</a></li>
<li><a href="day2.html">Presentation of Tuesday</a></li>
<li><a href="day3.html">Presentation of Wednesday</a></li>
<li><a href="day4.html">Presentation of Thursday</a></li>
<li><a href="day5.html">Presentation of Friday</a></li>
</ul>
</div>
<div class="right">
<b>Week 2</b>
<ul>
<li><a href="day6.html">Presentation of Monday</a></li>
<li><a href="day7.html">Presentation of Tuesday</a></li>
<li><a href="day8.html">Presentation of Wednesday</a></li>
<li><a href="day9.html">Presentation of Thursday</a></li>
</ul>
</div>
</section>
</div></div>
<div class="progress"><div></div></div>
<script src="scripts/script.js"></script>
<!-- Copyright © 2013 Florian Rappl, www.florian-rappl.de -->
</body>
</html>