-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.html
More file actions
403 lines (403 loc) · 14.8 KB
/
day2.html
File metadata and controls
403 lines (403 loc) · 14.8 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!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 2: Object oriented programming (OOP), Generics and Windows Forms projects</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>Again: Difference <code>class</code> and <code>struct</code></li>
<li>Properties</li>
<li>Constructor and inheritance</li>
<li>Keywords and syntax</li>
<li>Polymorphism</li>
<li>Modifiers</li>
<li>Implementing interfaces</li>
<li>Generic classes</li>
<li>Example of OOP: Windows Forms</li>
</ul>
</section>
</div></div>
<div class="slide" id="struct-class"><div>
<section>
<header>
<h2>Structures and classes</h2>
</header>
<ul>
<li>Structures are value types and will be copied</li>
<li>They are non-nullable (no instantiation required)</li>
<li>Classes are reference types and will be referenced</li>
<li>They are <code>null</code> if not instantiated</li>
<li>Polymorphism is possible with both</li>
<li>Structures can only inherit from interfaces</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-struct-class"><div>
<section>
<header>
<h2 class="example">→ Example - Structures and classes</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day2/Structures.cs" title="Download source" class="example-download">Structures.cs</a>
</section>
</div></div>
<div class="slide" id="constructor"><div>
<section>
<header>
<h2>Constructor</h2>
</header>
<ul>
<li>The constructor is always called</li>
<li>If no constructor is specified, a default one is used</li>
<li>Constructors can call other constructors of the same class</li>
<li>Or a constructor of the base class</li>
<li>By default the default constr. of the base class is called</li>
<li>Calling order: Call the other constructor, then invoke local code</li>
</ul>
</section>
</div></div>
<div class="slide" id="properties-1"><div>
<section>
<header>
<h2>Properties (pt. 1)</h2>
</header>
<ul>
<li>Variables should usually be only modified within a class</li>
<li>However, what if we want read access from outside?</li>
<li>Solution C++: Create method with a name starting with <i>Get</i></li>
<li>Solution C#: (other methods see it like a variable)
<pre><code>int myVariable;</code>
<code>public int MyVariable { get { return myVariable; } }</code></pre>
</li>
<li>This is called a property (possible: <code>get</code> and <code>set</code>)</li>
</ul>
</section>
</div></div>
<div class="slide" id="properties-2"><div>
<section>
<header>
<h2>Properties (pt. 2)</h2>
</header>
<pre><code>public int MyVariable { </code>
<code> get { return myVariable; } </code>
<code> set { myVariable = value; /* More code ? */ } </code>
<code>}</code></pre>
<ul>
<li>The big advantage in the <code>set</code> part - we can validate values, invoke events or function calls and control the state of our classes</li>
<li>Less code maintenance required</li>
</ul>
</section>
</div></div>
<div class="slide" id="auto-properties"><div>
<section>
<header>
<h2>Automatic property generation</h2>
</header>
<ul>
<li>Obviously the C# compiler does a lot for us</li>
<li>Writing variables and their properties is still some work</li>
<li>Another area where the C# compiler can help us</li>
<li>Consider: <code>int a; public int A { /* ... */ }</code></li>
<li>How about: <code>public int A { get; set; }</code></li>
<li>Compiler generates the member variable and associates it</li>
<li>Advantage: Sufficient for most cases</li>
<li>Disadvantage: Not helpful if we need value validation and more</li>
</ul>
</section>
</div></div>
<div class="slide" id="properties-with-abstract"><div>
<section>
<header>
<h2>Warning about auto properties</h2>
</header>
<ul>
<li>The syntax is the same as with <code>abstract</code> properties</li>
<li>The only difference is the <code>abstract</code> keyword</li>
<li>Auto prop. are very good in data-encapsulation classes</li>
<li>However, they have drawbacks in some cases (as seen)</li>
<li>Biggest drawback: We have no direct access to the variable</li>
<li>They are a good starter, which can be replaced easily</li>
<li>Advice: Use as often as possible, but do not hesitate to extend the code by changing it to a manual property</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-auto-properties"><div>
<section>
<header>
<h2 class="example">→ Example - Automatic properties</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day3/AutoProp.cs" title="Download source" class="example-download">AutoProp.cs</a>
</section>
</div></div>
<div class="slide" id="example-properties"><div>
<section>
<header>
<h2 class="example">→ Example - Properties</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day1/Properties.cs" title="Download source" class="example-download">Properties.cs</a>
</section>
</div></div>
<div class="slide" id="inheritance"><div>
<section>
<header>
<h2>Inheritance</h2>
</header>
<ul>
<li>In C# a class can only inherit from one class</li>
<li>Structures and classes can implement <i>n</i> interfaces</li>
<li>Access the classes members with the <code>this</code> pointer</li>
<li>Exclusively access base class members with <code>base</code></li>
<li>Modifiers are very important</li>
<li>Interfaces do not need modifiers, all <code>public</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="constructor-inheritance"><div>
<section>
<header>
<h2>Inheritance flow</h2>
</header>
<img src="pictures/ctor.png" class="middle r">
</section>
</div></div>
<div class="slide" id="modifiers"><div>
<section>
<header>
<h2>Modifiers</h2>
</header>
<ul>
<li>Access only from current class: <code>private</code></li>
<li>Access also from inherited classes: <code>protected</code></li>
<li>Access also from whole library: <code>internal</code></li>
<li>Access to everyone: <code>public</code></li>
<li>Additionally: <code>internal protected</code>, i.e. internal <i>or</i> protected</li>
<li>Default: <code>class</code> / <code>struct</code> / <code>interface</code> / <code>delegate</code> / <code>enum</code> is <code>internal</code>, a member of a class or structure is <code>private</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="polymorphism"><div>
<section>
<header>
<h2>Polymorphism</h2>
</header>
<ul>
<li>Hiding is always possible</li>
<li>Recommended technique: mark method as <code>new</code></li>
<li>New implementation only if marked as <code>abstract</code> (no basic implementation available) or <code>virtual</code></li>
<li>Mark new implementation as <code>override</code></li>
<li>Childs need to specify base constructor if no def. constructor available</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-constructor-inheritance"><div>
<section>
<header>
<h2 class="example">→ Example - Inheritance and polymorphism</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day2/Polymorphism.cs" title="Download source" class="example-download">Polymorphism.cs</a>
</section>
</div></div>
<div class="slide" id="interfaces"><div>
<section>
<header>
<h2>Interfaces</h2>
</header>
<ul>
<li>Interfaces are like contracts</li>
<li>They say nothing about internal structures</li>
<li>Usages: ability to have multi-inheritance and specifying abilities</li>
<li>Can be implemented implicit (like members) or explicit (access only if casted to the specific interface type)</li>
<li>Interfaces can inherit from other interfaces</li>
</ul>
</section>
</div></div>
<div class="slide" id="abstract"><div>
<section>
<header>
<h2>Abstract classes</h2>
</header>
<ul>
<li>Abstract class is like an interface with implementations</li>
<li>Just use <code>abstract</code> before <code>class</code></li>
<li>This enables the usage of <code>abstract</code> methods and properties</li>
<li>Such members have to be implemented</li>
<li>No instances of abstract classes can be created</li>
<li>Use it if you want something stronger than a contract</li>
<li>Keep in mind: A class can only inherit from one other class</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-abstract-interfaces"><div>
<section>
<header>
<h2 class="example">→ Example - Shaping classes</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day2/Interfaces.cs" title="Download source" class="example-download">Interfaces.cs</a><br>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day2/FullExampleClasses.cs" title="Download source" class="example-download">FullExampleClasses.cs</a>
</section>
</div></div>
<div class="slide" id="generic-methods"><div>
<section>
<header>
<h2>Repetition: Generic methods</h2>
</header>
<ul>
<li>Generics is a way of reusing code</li>
<!--<li>Not only classes can be generated, also methods</li>-->
<!--<li>Similar syntax, but (mostly) different (easier) usage</li>-->
<li>For methods the compiler can (mostly) infer the type</li>
<li>In the following example also note the use of <code>ref</code></li>
</ul>
<pre><code>public static void <mark>Swap<mark class="important"><T></mark></mark>(ref T l, ref T r) {</code>
<code> T temp = r; r = l; l = temp;</code>
<code>}</code>
<code>int a = 3; int b = 4; <mark>Swap</mark>(ref a, ref b);</code></pre>
</section>
</div></div>
<div class="slide" id="generics"><div>
<section>
<header>
<h2>Generic classes</h2>
</header>
<ul>
<!--<li>Generics is another way of reusing code</li>-->
<li>The goal is to generate (similar) classes that differ in one or more types</li>
<li>Best example: <code>List<T></code> - strongly typed collection (list)</li>
<li>Compiler generates the classes for all types that are used in our code</li>
<li>If we use <code>List<int></code>, <code>List<double></code>, two classes will be generated by the compiler</li>
<li>Requirement: Specifying the type identifiers (like method arguments) and using them (e.g. name <code>T</code> with usage <code>T myvariable</code>)</li>
</ul>
</section>
</div></div>
<div class="slide" id="compiler-generated"><div>
<section>
<header>
<h2>Compiler generated classes</h2>
</header>
<img src="pictures/generics.png" class="r middle">
</section>
</div></div>
<div class="slide" id="generic-constraints"><div>
<section>
<header>
<h2>Generic constraints</h2>
</header>
<ul>
<li>Generics alone are powerful, yet very limited</li>
<li>By setting constraints some limitations can be overcome</li>
<li>The magic keyword is <code>where</code></li>
<li>Several possibilities e.g. <code>new()</code> means has def. constructor</li>
<li>One example: <code>void Test<T>(T obj) where T : new()</code></li>
<li>More constraints possible, e.g. <code>T : Stopwatch, new()</code></li>
<li>Constraints with multiple types? Separate with <code>where</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="example-generics"><div>
<section>
<header>
<h2 class="example">→ Example - Generics</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day2/Generics.cs" title="Download source" class="example-download">Generics.cs</a>
</section>
</div></div>
<div class="slide" id="windows-forms"><div>
<section>
<header>
<h2>Windows Forms</h2>
</header>
<ul>
<li>One of several GUI possibilities</li>
<li>First one implemented in .NET</li>
<li>Start a project: File, New, Project, C#, Windows Forms Application</li>
<li>The integrated designer will help us</li>
<li>There is a big OOP tree behind this</li>
<li>For us most important is <code>Control</code></li>
<li>A really important derivative is <code>Form</code></li>
<li><code>Form</code> represents a window</li>
</ul>
</section>
</div></div>
<div class="slide" id="gui"><div>
<section>
<header>
<h2>Understanding GUI</h2>
</header>
<ul>
<li>We need a loop that performs updates</li>
<li>That loop is actually integrated in Windows, we just need to start it</li>
<li>Windows will then register our app and post messages</li>
<li>Such messages arrive in our app as events</li>
<li>An event can be something like mouse over, click, key down, ...</li>
<li>The state of controls is based on such events (like hover)</li>
<li>Additionally we can register our own handlers</li>
<li>Right now its enough to do it with the designer</li>
</ul>
</section>
</div></div>
<div class="slide" id="message-loop"><div>
<section>
<header>
<h2>The message loop</h2>
</header>
<img src="pictures/messageloop.png" class="r middle">
</section>
</div></div>
<div class="slide" id="example-windows-forms"><div>
<section>
<header>
<h2 class="example">→ Example - Windows Forms</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day2/SimpleForm.cs" title="Download source" class="example-download">SimpleForm.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>