-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
1367 lines (1207 loc) · 53.9 KB
/
index.html
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>C# Programming Language</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/league.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/docco.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="start" data-transition="zoom">
<h1 style="margin-bottom: 0;">C#</h1>
<h3>
<span style="background: rgba(0,0,0,0.2); padding: .5rem 2rem; border-radius: 0.5rem;">
Programming Language
</span>
</h3>
<div style="font-size: 80%; margin-top: 5rem;">
<div style="line-height: 1rem;">Mohammad Dehghan</div>
<span style="font-size:0.7em">
<a href="mailto:[email protected]">[email protected]</a><br />
</span>
<div class="social">
<a href="https://stackoverflow.com/users/1174942/mohammad-dehghan" target="_blank">
<img src="img/stackoverflow-blue.png" style="width: 20px; height: 20px" />
</a>
<a href="https://github.com/prodehghan" target="_blank">
<img src="img/GitHub-Mark-blue.png" style="width: 20px; height: 20px" />
</a>
<a href="https://twitter.com/prodehghan" target="_blank">
<img src="img/twitter-blue.png" style="width: 20px; height: 20px" />
</a>
</div>
<div style="font-size: 90%; width: 30rem; padding-top: 3rem; margin: 3rem auto 0 auto; border-top: 1px dashed rgba(255,255,255,0.3);">
<span style="font-size:1em; display: block; line-height: 1rem;">SystemGroup</span>
<a href="https://www.systemgroup.net" target="_blank" style="font-size: 70%;">www.systemgroup.net</a>
<br />
<img src="img/sg-logo.png" style="width: 15rem; border: none; background: transparent; box-shadow: none;">
</div>
</div>
</section>
<section>
<section class="items">
<h2>C# Programming Language</h2>
<ul>
<li>General-purpose, type-safe, object-oriented</li>
<li>Supports functional programming</li>
<li>Supports dynamic typing</li>
<li>Supports declarative programming</li>
<li>Supports asynchronous programming</li>
</ul>
</section>
<section class="items">
<h2>Object Orientation</h2>
<ul>
<li>Unified type system</li>
<li>Classes and interfaces</li>
<li>Methods, properties, events</li>
</ul>
</section>
<section class="items">
<h2>Type Safety</h2>
<ul>
<li>Compile-time static typing</li>
<li>Run-time type safety</li>
<li>Strongly typed</li>
<li><code class="blue"></code>dynamic</code> keyword</li>
</ul>
</section>
<section class="items">
<h2>Functional Programming</h2>
<ul>
<li>Functions as values (delegate)</li>
<li>Anonymous methods and lambda expressions</li>
<li>Captured local variables</li>
</ul>
</section>
<section class="items">
<h2>
Declarative Programming
</h2>
<ul>
<li>
Language-integrated query (LINQ)
<ul>
<li>Fluent-syntax (Extension methods and lambda expressions)</li>
<li>Query-syntax (declarative)</li>
</ul>
</li>
</ul>
</section>
<section class="items">
<h2 style="width: 130%">Asynchronous Programming</h2>
<ui>
<li>Tasks</li>
<li>async/await</li>
</ui>
</section>
<section class="items">
<h2>Memory Management</h2>
<ul>
<li>Garbage collector</li>
<li>No <code class="blue">delete</code> keyword</li>
</ul>
</section>
<section data-background-image="img/ms.net-background.png" data-background-transition="zoom">
</section>
<section class="items">
<h2>.NET Platform</h2>
<ul>
<li>Development tools (compiler)</li>
<li>Runtime services (CLR)</li>
<li>Class library</li>
</ul>
</section>
<section class="items">
<h2>.NET Platform Variations</h2>
<ul>
<li>.NET Framework</li>
<li>.NET Core</li>
<li>Mono</li>
<li>Xamarin</li>
</ul>
</section>
<section data-background-color="#360">
<h2>Let's do it!</h2>
</section>
</section>
<section>
<section>
<h2>Creating first application</h2>
<ul>
<li>Install .NET Core</li>
<li>Open a terminal window<br /> (PowerShell/Command Prompt/bash)</li>
<pre class="bash"><code data-trim>
$ md HelloCS
$ cd HelloCS
$ dotnet new console
$ dotnet run
</code></pre>
</ul>
</section>
<section>
<h2 style="width: 130%; margin-left: -15%">Working With Visual Studio Code</h2>
<div class="left">
<ul>
<li>Install Visual Studio Code </li>
<li>Install C# extension</li>
<li>Open project folder</li>
<ul>
<li>Let the C# extension install required files</li>
<li>VS Code prompts you about adding required assets to build and debug. Click
"Yes".</li>
</ul>
</ul>
</div>
</section>
<section>
<img src="img/vscode-csintall.png" />
</section>
<section>
<img src="img/vscode-debugassets.png" />
</section>
</section>
<section>
<h2>Analyzing the program</h2>
<div data-block-type="code" class="sl-block">
<div class="sl-block-content">
<pre class="cs">
<code data-trim>
using System;
namespace HelloCS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
</code>
</pre>
</div>
</div>
</section>
<section>
<h2><code class="blue">Main</code> Method</h2>
<ul>
<li>Starting point of an executable</li>
<li>Passing command line arguments</li>
</ul>
</section>
<section>
<h2>Namespaces</h2>
<ul>
<li>Namespaces organize types</li>
<li>Avoid type name collisions</li>
<li><code class="blue">using</code> directive brings other namespaces’ types into scope</li>
<li>Type full name</li>
</ul>
</section>
<section>
<h2>Types</h2>
<ul>
<li>C# is strongly typed</li>
<li>Everything has a type</li>
<li>We can define our own types</li>
<li>Code you want to execute must live inside a type</li>
</ul>
</section>
<section>
<h2>Statements and Expressions</h2>
<div class="left">
<ul style="font-size: 0.9em">
<li>A statement is an instruction
<ul>
<li>A method is a series of instructions</li>
<li>Statements end with semicolon</li>
<li>Statements are executed in order they appear</li>
</ul>
</li>
<li>
Expressions are parts of statements that produce a value
<ul>
<li>The may involve operators</li>
</ul>
</li>
</ul>
</div>
</section>
<section>
<h2>Objects</h2>
<div class="left">
<ul>
<li>Everything is an object</li>
<li>Objects have members</li>
<li>Members are accessed by dot (.) operator</li>
</ul>
</div>
</section>
<section>
<h2>Variables</h2>
<div class="left">
Variables hold a value
<ul style="font-size: 0.8em">
<li>They always have a type</li>
<li>They must be assigned a value before they can be used</li>
<li>Variable’s type usually determines how much memory it consumes</li>
</ul>
</div>
<pre class="cs">
<code data-trim>
int i;
float x, y;
string name = "Mohammad";
</code>
</pre>
</section>
<section>
<section>
<h2>Numeric Types (Part One)</h2>
<div class="left">
Integers<br />
<ul>
<li>Signed (int, long, short, sbyte)</li>
<li>Unsigned (uint, ulong, ushort, byte)</li>
</ul>
</div>
<pre class="cs"> <code data-trim>
byte b = 30;
int i = 12000 + b;
i = 10 / 3;
short s = 762190; // Error: Constant value '762190'
// cannot be converted to a 'short'
</code></pre>
</section>
<section>
<h2>Numeric Types (Part Two)</h2>
<div class="left">
Real numbers<br />
<ul>
<li>Floating point (float, double)
<ul style="font-size: 0.8em">
<li>
Typically used for scientific and graphical calculations
</li>
</ul>
</li>
<li>Decimal (decimal)
<ul style="font-size: 0.8em">
<li>
Typically used for financial calculations, where base-10-accurate
arithmetic and high precision are required</li>
</ul>
</li>
</ul>
</div>
<pre class="cs"><code data-trim>
double pi = 3.1415926535897932384; // 3.14159265358979
float fpi = (float)pi; // 3.141593
double x = 10 / 3; // 3!
</code></pre>
</section>
<section>
<h2>Numeric Literals</h2>
<pre class="cs"><code data-trim>
int million = 1_000_000; // underscore can be
// inserted anywhere (C# 7)
int x = 0x7f; // 0x prefix: hexadecimal literal
byte b = 0b10000000; // 0b prefix: binary literal (C# 7)
byte b2 = 0b1000_1000;
double d = 1.5;
double million2 = 1e06;
float f = 1.2f;
decimal dec = -1.23M; // Will not compile without the M suffix.
</code></pre>
</section>
<section>
<h2>Numeric Conversions</h2>
<pre class="cs"><code data-trim>
int x = 12345; // int is a 32-bit integer
long y = x; // Implicit conversion to 64-bit integral type
short z = (short)x; // Explicit conversion to 16-bit integral type
int i = 10;
decimal d = i;
int j = (int)d;
i = (int)1.7; // 1 - fractional portion is truncated
float f = 1.1f;
d = (decimal)f;
double d2 = (double)d;
</code></pre>
</section>
<section>
<h2>Arithmetic Operators</h2>
<table>
<tr>
<td> + </td>
<td> Addition </td>
</tr>
<tr>
<td> - </td>
<td> Subtraction </td>
</tr>
<tr>
<td> * </td>
<td> Multiplication </td>
</tr>
<tr>
<td> / </td>
<td> Division </td>
</tr>
<tr>
<td> % </td>
<td> Remainder after division </td>
</tr>
<tr>
<td> ++</td>
<td> Increment </td>
</tr>
<tr>
<td> --</td>
<td> Decrement </td>
</tr>
</table>
<br />
<div style="font-size: 0.5em" class="left">Skipped: overflow and check operators, bitwise operators
</div>
</section>
<section>
<h2 style="width: 120%; margin-left: -10%">Pre/Post Increment/Decrement</h2>
<pre class="cs"><code data-trim>
int x = 0, y = 0;
Console.WriteLine (x++); // Outputs 0; x is now 1
Console.WriteLine (++y); // Outputs 1; y is now 1
</code></pre>
</section>
</section>
<section>
<h2>Boolean</h2>
<div class="left">
<ul>
<li>true or false</li>
<li>Result of relational operators</li>
</ul>
</div>
<pre class="cs"><code data-trim>
float temperature = 20;
bool isHot = temperature > 35;
bool rainy = true;
bool sunny = false;
bool windy = true;
bool useUmbrella = !windy && (rainy || sunny);
</code></pre>
</section>
<section>
<section>
<h2>Characters</h2>
<div class="left">
<ul>
<li>
Characters are Unicode
</li>
<li>
Escape sequences
<pre class="cs"><code data-trim>
char newLine = '\n';
char backSlash = '\\'
</code></pre>
</li>
</ul>
</div>
</section>
<section>
<h2>Strings</h2>
<pre class="cs"><code data-trim>
string a = "Here's a tab:\t";
string a1 = "\\\\server\\fileshare\\helloworld.cs";
// Verbatim string literal
string a2 = @"\\server\fileshare\helloworld.cs";
int l = s2.Length;
</code></pre>
</section>
<section>
<h2>String interpolation (C# 6)</h2>
<div class="left">
<pre class="cs"><code data-trim>
int x = 4;
Console.Write ($"A square has {x} sides");
</code></pre>
</div>
</section>
<section>
<h2>String Operators</h2>
<div class="left">
<ul>
<li>
Concatenation (+)
</li>
<li>
Equality (==)
</li>
<li>
Indexer (<code>[]</code>)
</li>
<li>
Instance and static methods (CompareTo, Split, Substring, ...)
</li>
</ul>
</div>
</section>
</section>
<section>
<section>
<h2> C# Built-In Types Summary</h2>
<ul style="font-size: 0.8em">
<li>
Value types
<ul>
<li>
Numeric
<ul>
<li>Signed integer (sbyte, short, int, long)</li>
<li>Unsigned integer (byte, ushort, uint, ulong)</li>
<li>Real number (float, double, decimal)</li>
</ul>
</li>
<li>Logical (bool)</li>
<li>Character (char)</li>
</ul>
</li>
<li>
Reference types
<ul>
<li>String (string)</li>
<li>Object (object)</li>
</ul>
</li>
<li>
Primitive types: Predefined values types except decimal
</li>
</ul>
</section>
<section>
<h2>C# Built-In Types</h2>
<table style="font-size: 0.6em">
<thead>
<tr>
<th>C# Type</th>
<th>.NET Framework Type</th>
<th>C# Type</th>
<th>.NET Framework Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>bool</td>
<td><strong>System.Boolean</strong></td>
<td>unit</td>
<td><strong>System.UInt32</strong></td>
</tr>
<tr>
<td>byte</td>
<td><strong>System.Byte</strong></td>
<td>long</td>
<td><strong>System.Int64</strong></td>
</tr>
<tr>
<td>sbyte</td>
<td><strong>System.SByte</strong></td>
<td>ulong</td>
<td><strong>System.UInt64</strong></td>
</tr>
<tr>
<td>char</td>
<td><strong>System.Char</strong></td>
<td>object</td>
<td><strong>System.Object</strong></td>
</tr>
<tr>
<td>decimal</td>
<td><strong>System.Decimal</strong></td>
<td>short</td>
<td><strong>System.Int16</strong></td>
</tr>
<tr>
<td>double</td>
<td><strong>System.Double</strong></td>
<td>ushort</td>
<td><strong>System.UInt16</strong></td>
</tr>
<tr>
<td>float</td>
<td><strong>System.Single</strong></td>
<td>string</td>
<td><strong>System.String</strong></td>
</tr>
<tr>
<td>int</td>
<td><strong>System.Int32</strong></td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
</section>
</section>
<section>
<h2>Control Statements</h2>
<div class="left">
<ul>
<li>
if/else
</li>
<li>
switch
<ul>
<li>Only numbers, characters, strings and booleans</li>
<li><code>case</code> labels are constants</li>
<li><code>default</code> label is optional</li>
<li>Cannot fall through another case, unless it has no statements</li>
<li><code>goto case</code> / <code>goto default</code></li>
</ul>
Conditional expression (?:)
</li>
</ul>
</div>
</section>
<section>
<section>
<h2>Arrays</h2>
<div class="left">
<p>
Multiple variables under the same name
</p>
<ul style="font-size: 0.9em">
<li>Accessible through index</li>
<li>Index starts from zero</li>
<li>All element have the same type</li>
<li><code class="blue">new</code> is used to create an array</li>
<li><code>Length</code> property</li>
<li>May have more than one dimension
<ul>
<li>
<code>Length</code> property gives the total length.
</li>
<li><code>GetLength(<dimension>)</code></li>
</ul>
</li>
<li>May be jagged</li>
</ul>
</div>
</section>
<section data-transition="convex-in fade-out">
<h2>Array Initialization</h2>
<pre class="cs"><code data-trim>
char[] vowels; // Declare an array of characters
vowels = new char[5]; // Initialize with an array of 5 characters
// Fill with values
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
</code></pre>
</section>
<section data-transition="fade">
<h2>Array Initialization</h2>
<pre class="cs"><code data-trim>
char[] vowels;
vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
</code></pre>
or
<pre class="cs"><code data-trim>
char[] vowels;
vowels = new [] { 'a', 'e', 'i', 'o', 'u' };
</code></pre>
or
<pre class="cs"><code data-trim>
char[] vowels = {'a','e','i','o','u'};
</code></pre>
</section>
<section data-transition="fade">
<h2>Array Initialization</h2>
<pre class="cs"><code data-trim>
int[,] matrix =
{
{0,1,2},
{3,4,5},
{6,7,8}
};
</code></pre>
<pre class="cs"><code data-trim>
int[][] matrix = new int[][]
{
new int[] {0,1,2},
new int[] {3,4,5},
new int[] {6,7,8,9}
};
</code></pre>
</section>
</section>
<section>
<section>
<h2>Methods - Basics</h2>
<div class="left" style="font-size: 0.9em">
<ul>
<li>Methods define operations and behaviors</li>
<li>Every method has a return type
<ul>
<li><code>void</code> if no return value</li>
<li>return is used to exit the method and/or return a value</li>
<li>Multiple return statements are allowed</li>
</ul>
</li>
<li>Every method has zero or more parameters</li>
<li>Methods can be called recursively</li>
<li>Variables defined inside a method are called local variables</li>
<li>Methods can be overloaded</li>
</ul>
</div>
</section>
<section>
<h2>Method Overloads</h2>
<pre class="cs"><code data-trim style="max-height: unset">
static float CalculateWeight(float mass, float gravity)
{
return mass * gravity;
}
static float CalculateWeight(float mass)
{
return CalculateWeight(mass, 9.807f);
}
static void Main()
{
float mass = 10;
float weightOnEarth = CalculateWeight(mass);
float weightOnMars = CalculateWeight(mass, 3.711f);
}
</code></pre>
</section>
<section>
<h2>Methods Parameters</h2>
<div class="left">
<ul>
<li>They are local to the method</li>
<li>By default, method parameters are "pass by value"</li>
<li>Can be optional (by having a default value)</li>
<li><code>ref</code> modifier: "pass by reference" - in/out parameter</li>
<li><code>out</code> modifier: output parameter (also "pass by reference")</li>
</ul>
</div>
</section>
<section>
<h2>Optional Parameters</h2>
<pre class="cs"><code data-trim>
static void Main()
{
float mass = 10;
float weightOnEarth = CalculateWeight(mass);
float weightOnMars = CalculateWeight(mass, 3.711f);
}
static float CalculateWeight(float mass, float gravity = 9.807f)
{
return mass * gravity;
}
</code></pre>
</section>
<section>
<h2>Named Arguments</h2>
<pre class="cs"><code data-trim>
void Foo(int x, int y)
{
Console.WriteLine(x + ", " + y);
}
void Test()
{
Foo(x: 1, y: 2); // 1, 2
Foo(y: 2, x: 1); // 1, 2
}
</code></pre>
</section>
<section data-transition="convex-in fade-out">
<h2>The <code>ref</code> Modifier</h2>
<pre class="cs"><code data-trim>
static void Foo(ref int p)
{
p = p + 1; // Increment p by 1
Console.WriteLine(p); // Write p to screen
}
static void Main()
{
int x = 8;
Foo(ref x); // Ask Foo to deal directly with x
Console.WriteLine(x); // x is now 9
}
</code></pre>
</section>
<section data-transition="fade-in convex-out">
<h2>The <code>ref</code> Modifier</h2>
<pre class="cs"><code data-trim>
static void Swap(ref string a, ref string b)
{
string temp = a;
a = b;
b = temp;
}
</code></pre>
</section>
<section>
<h2>The <code>out</code> Modifier</h2>
<div class="left">
Like <code>ref</code> modifier, but:
<ul>
<li>Need not be assigned before going into the function</li>
<li>Must be assigned before it comes out of the function</li>
</ul>
</div>
<pre class="cs"><code data-trim>
Console.WriteLine("Please enter an integer:");
int number;
while(true)
{
string s = Console.ReadLine();
if(int.TryParse(s, out number))
break;
Console.WriteLine("Not a valid number! Try again:");
}
</code></pre>
</section>
<section>
<h3>Out Variable Declaration (C# 7)</h3>
<pre class="cs"><code data-trim>
if(double.TryParse(str, out double d))
{
// do something
}
</code></pre>
<h3>Out Discards (C# 7)</h3>
<pre class="cs"><code data-trim>
static bool IsDateValid(string dateString)
{
return DateTime.TryParse(dateString, out _);
}
</code></pre>
</section>
<section>
<h3>Implicitly Typed Local Variable</h3>
<ul>
<li><code class="blue">var</code> keyword</li>
<li>Must be assigned a value on declaration</li>
<li>Statically typed</li>
<li>They are mandatory when using anonymous types</li>
</ul>
<pre class="cs" style="width: 100%"><code data-trim>
var i = 10;
i = 1.1; // Error: Cannot implicitly convert type 'double' to 'int'.
var text = Console.ReadLine();
var words = text.Split();
var n; // Error!
var x = 1, y = 10; // Error!
</code></pre>
</section>
<section>
<h2>The <code>params</code> Modifier</h2>
<pre class="cs" style="zoom: 90%"><code data-trim >
static int Sum(params int[] ints)
{
int sum = 0;
for (int i = 0; i < ints.Length; i++)
sum += ints[i]; // Increase sum by ints[i]
return sum;
}
static void Main()
{
int total = Sum(1, 2, 3, 4);
Console.WriteLine(total); // 10
}
</code></pre>
<div class="left">
<ul>
<li>Allows methods to accept any number of arguments</li>
<li>Must be defined as an array</li>
<li>Must be the last parameter</li>
</ul>
</div>
</section>
<section>
<h2>Local Variable Scope</h2>
<div class="left">
<ul>
<li>Local variables are accessible after definition</li>
<li>Every code block defines a scope for local variables</li>
<li>Local variables defined in outer scope are accessible in inner scope</li>
<li>A variable cannot be defined with the same name as another local variable in outer
scope</li>
</ul>
</div>
</section>
<section>
<pre class="cs"><code data-trim style="max-height: unset">
static void Main(string[] args)
{
int a = 1;
{
int x = 10, y = 5;
Console.WriteLine(x + " " + y);
int a; // Error. Already defined in outer scope
}
{
int x = 30; // Valid.
Console.WriteLine(a); // Valid.
Console.WriteLine(x); // Valid.
Console.WriteLine(y); // Error. Not accessible
}
}
</code></pre>
</section>
</section>
<section>
<h2>Jumping Statements</h2>
<div class="left">