-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.html
More file actions
348 lines (348 loc) · 12.3 KB
/
day5.html
File metadata and controls
348 lines (348 loc) · 12.3 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
<!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 5: IO, webrequests, events, serializing objects and dynamic types</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>Accessing the file system</li>
<li>Streams</li>
<li>Performing web requests</li>
<li>Serializing data in C#</li>
<li>The XML serializer</li>
<li>The IDisposable interface</li>
<li>Releasing resources</li>
<li>The C# event system</li>
</ul>
</section>
</div></div>
<div class="slide" id="file-system"><div>
<section>
<header>
<h2>Accessing the file system</h2>
</header>
<ul>
<li>Task: Get information about a directory</li>
<li>Or tell me everything about a file</li>
<li>We need to read out information of the FS</li>
<li>Luckily Windows knows everything and has good APIs</li>
<li>The .NET-Framework opens those APIs for us</li>
<li>Important: When handling files think about threading</li>
<li>We do not want to block the UI thread</li>
</ul>
</section>
</div></div>
<div class="slide" id="dotnet-fs"><div>
<section>
<header>
<h2>The <code>System.IO</code> namespace</h2>
</header>
<ul>
<li>The static helper class <code>Path</code></li>
<li>Two important classes: <code>Directory</code> and <code>File</code></li>
<li>Encapsulations like <code>DirectoryInfo</code>, <code>FileInfo</code></li>
<li>Or more specialized <code>DriveInfo</code></li>
<li>All kinds of streams (memory, file, text, ...)</li>
<li>The whole ACL model</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-file-systems"><div>
<section>
<header>
<h2 class="example">→ Example - The file system</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day5/FileSystem" title="Download source" class="example-download">FileSystem.zip</a>
</section>
</div></div>
<div class="slide" id="streams"><div>
<section>
<header>
<h2>Streams</h2>
</header>
<ul>
<li>Usually every IO operation is about streaming data</li>
<li>Every stream of data is dependent of the corresponding device</li>
<li>e.g. HDD, RAM, ethernet, modem, ...</li>
<li>The abstract base class is <code>Stream</code></li>
<li>Sometimes it might make sense to implement it yourself</li>
<li>Reading a file has to do with streams</li>
</ul>
</section>
</div></div>
<div class="slide" id="reading-a-file"><div>
<section>
<header>
<h2>Reading a file</h2>
</header>
<ul>
<li>A specific implemention called <code>FileStream</code> exists</li>
<li><code>var fs = new FileStream("...", FileMode.Open)</code> will give us the stream</li>
<li>Now we can read a byte with <code>ReadByte()</code></li>
<li><code>Read()</code> will read as many bytes as specified into an array</li>
<li>Returns the number of read bytes (the end of the stream is a barrier)</li>
</ul>
</section>
</div></div>
<div class="slide" id="writing-a-file"><div>
<section>
<header>
<h2>Writing a file</h2>
</header>
<ul>
<li>Writing a file is quite similar - e.g. <code>FileMode.Create</code></li>
<li>Here we have methods like <code>WriteByte()</code> and <code>Write()</code></li>
<li>Very important: After our operations we have to close the file</li>
<li>This happens with the method <code>Close()</code></li>
<li>Depending on the device bytes could be buffered before any actual write</li>
<li>If immediate writing is required use the <code>Flush()</code> method</li>
<li>Always use <code>ReadAsync()</code> and <code>WriteAsync()</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="streamreader-streamwriter"><div>
<section>
<header>
<h2><code>StreamReader</code>, <code>StreamWriter</code></h2>
</header>
<ul>
<li>They are implementations of <code>TextReader</code>, <code>TextWriter</code></li>
<li>Their purpose is elegant handling of text</li>
<li>They expect a <code>Stream</code> (or create a <code>FileStream</code>)</li>
<li>One can set a proper text encoding (like UTF8, Unicode, ...)</li>
<li>e.g. <code>var sw = new StreamWriter("...", false, Encoding.ASCII)</code></li>
<li>Here we have additionally <code>WriteLine()</code></li>
<li>All write methods accept strings or convert objects to strings</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-streams"><div>
<section>
<header>
<h2 class="example">→ Example - Memory and file streams</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day5/DragAndDrop" title="Download source" class="example-download">DragAndDrop.zip</a>
</section>
</div></div>
<div class="slide" id="webrequests"><div>
<section>
<header>
<h2>Web requests</h2>
</header>
<ul>
<li>Accessing the internet is quite easy</li>
<li><code>WebRequest.Create()</code> creates a new web request</li>
<li>Here not blocking the UI is very important</li>
<li>We can directly query URLs and more</li>
<li>Direct access to the request stream (to the server)</li>
<li>We first need to request a response object</li>
<li>Then the request is completed</li>
<li>The response stream (answer) is then received (from the server)</li>
</ul>
</section>
</div></div>
<div class="slide" id="simple-webrequest"><div>
<section>
<header>
<h2>A simple web request</h2>
</header>
<pre>
<code>var request = WebRequest.Create(url);</code>
<code>var response = await request.GetResponseAsync();</code>
<code>var stream = new StreamReader(response.GetResponseStream());</code>
<code>var content = stream.ReadToEnd();</code>
</pre>
</section>
</div></div>
<div class="slide" id="example-webrequests"><div>
<section>
<header>
<h2 class="example">→ Example - A webrequest with C#</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day5/WebRequest.cs" title="Download source" class="example-download">WebRequest.cs</a>
</section>
</div></div>
<div class="slide" id="idisposable"><div>
<section>
<header>
<h2>The <code>IDisposable</code> interface</h2>
</header>
<ul>
<li>Many methods have to mark resources as being in use</li>
<li>The question is: How to release such resources?</li>
<li>Apparently the <code>Close()</code> method is important</li>
<li>When automatically releasing resources, how does the GC know?</li>
<li>The GC will automatically call the <code>Dispose()</code> method</li>
<li>The method is defined in the interface <code>IDisposable</code></li>
<li>So this is our chance to tell the GC what to do</li>
<li>Additionally we have a destructor, but that is uninteresting</li>
</ul>
</section>
</div></div>
<div class="slide" id="releasing-resources"><div>
<section>
<header>
<h2>Releasing resources</h2>
</header>
<ul>
<li>We can explicitly release resources using a <code>using</code> construct</li>
<li>The syntax is quite close to <code>foreach</code></li>
<li>We could also call <code>Dispose()</code> directly</li>
</ul>
<pre>
<code>using(var fs = new FileStream(/* ... */)) {</code>
<code> /* Do something */</code>
<code>} // Resource automatically closed and freed</code>
</pre>
</section>
</div></div>
<div class="slide" id="example-resource-management"><div>
<section>
<header>
<h2 class="example">→ Example - Resource management</h2>
</header>
</section>
</div></div>
<div class="slide" id="serializing"><div>
<section>
<header>
<h2>Serializing data</h2>
</header>
<ul>
<li>The <code>BitConverter</code> is a very useful class</li>
<li>The attribute <code>[Serializable]</code> is important</li>
<li>Two ways: objects to bytes and bytes to objects</li>
</ul>
</section>
</div></div>
<div class="slide" id="xml-serializer"><div>
<section>
<header>
<h2>XML serialization</h2>
</header>
<ul>
<li>Another way is the XML serialization</li>
<li>Advantage: Human readable and standardized</li>
<li>Disadvantage: Big files with overhead</li>
<li>Just use the <code>XmlSerializer</code> class</li>
<li>The method for serializing is <code>Serialize()</code></li>
<li>Object creation from XML files is also possible</li>
<li>Here we have to use the <code>Deserialize()</code> method</li>
</ul>
</section>
</div></div>
<div class="slide" id="binary-serializer"><div>
<section>
<header>
<h2>Binary serialization</h2>
</header>
<ul>
<li>To save objects in binary form, use the <code>BinaryFormatter</code></li>
<li>The data is transferred to or from a stream</li>
<li>This is useful to save objects e.g. to a file</li>
<li>The method for serializing is again <code>Serialize()</code></li>
<li>To obtain an object from a stream use <code>Deserialize()</code></li>
<li>Depending on the object a typecast is necessary</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-serialization"><div>
<section>
<header>
<h2 class="example">→ Example - Object serialization</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day5/Serialization" title="Download source" class="example-download">Serialization.zip</a>
</section>
</div></div>
<div class="slide" id="event-system"><div>
<section>
<header>
<h2>The C# event system</h2>
</header>
<ul>
<li>Important: The <code>event</code> keyword</li>
<li>Events are just delegates, but with benefits</li>
<li>Only accessible with the <code>+=</code>, <code>-=</code> operations (outside)</li>
<li>Inside we can execute them (called firing)</li>
<li><code>public event EventHandler MyEvent</code> to register</li>
<li>The delegate in this example is <code>EventHandler</code></li>
<li>.NET pattern: Arguments <code>object</code>, <code>EventArgs</code> (or derived)</li>
<li>The return type should be <code>void</code></li>
</ul>
</section>
</div></div>
<div class="slide" id="event-important"><div>
<section>
<header>
<h2>Important event facts</h2>
</header>
<ul>
<li>Firing an event is done in the current thread</li>
<li>If necessary: perform context switch</li>
<li>If the handler is not set, it will be <code>null</code> (check first!)</li>
<li>One should prefer using overrides instead of events</li>
<li>This is of course only possible when deriving from a class</li>
<li>Events are (mostly) fired synchronously</li>
</ul>
</section>
</div></div>
<div class="slide" id="example-events"><div>
<section>
<header>
<h2 class="example">→ Example - Events</h2>
</header>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day5/Event" title="Download source" class="example-download">Event.zip</a><br>
<a href="https://github.com/CSharpLecture/Samples/blob/master/day5/EventExample2.cs" title="Download source" class="example-download">Example 2</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>