-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsharpnotes.txt
553 lines (438 loc) · 13.9 KB
/
csharpnotes.txt
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
╔═══════════════════╗
║ GLOSSARY ║
╚═══════════════════╝
* using - a way to include other namespaces(modules), much like import in python
* namespace - a collection of classes
* class - contains methods and definitions of your program, typically multiple methods
* method - a collection of statements that are grouped together to perform an operation, also known as a function
* public - visibility - allows any part of the program in the same assembly or another assembly to access the type and its members
* private - restricts other parts of the program from accessing the type and its members, only code in the same class or struct can access it
* static - variables and methods that belong to a class instead of a specific instance, allows direct calls on class without objects
* internal - allows other program code in the same assembly to accesss the type or its members, default access if no modifier is specified
* protected - allows codes in the same class or a class that derives from that class to access the type or its members
* void - indicates that a method does not return a value
* main - entry point for the program, much like an init in python
* int - return type, instance
* string[] - a sequence of characters
* int[] - a sequence of integers
* List<int> - a list of integers
* argument - a peice of data that is passed into a method or program, also known as a parameter
* var - indicates the object will be a variable - can be an integer or a string
* const - store a value that cannot be changed from their initial assignment
* ++x - Prefix - increments the value, and then proceeds with the expression.
* x++ - Postfix - evaluates the expression and then performs the incrementing.
* switch - statement that provides a more elegant way to test a variable for equality against a list of values
- case - represents a value to be checked, followed by a colon
- default - code executes when none of the cases match the switch expression
╔═══════════════════╗
║ VALUE TYPES ║
╚═══════════════════╝
* bool - Boolean value - True or False
* byte - 8-bit unsigned integer - 0 to 255
* char - 16-bit unicode character
* decimal - 128-bit precise decimal value -
* double - 64-bit precision floating point -
* float = 32-bit precision floating point -
* int - 32-bit signed integer - -2,147,483,648 to 2,147,483,647
* long = 64-bit signed integer - -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
* sbyte - 8-bit signed integer - -128 to 127
* short - 16-bit signed integer - -32,768 to 32,767
* uint - 32-bit unsigned integer - 0 to 4,294,967,295
* ulong - 64-bit unsigned integer - 0 to 18,446,744,073,709,551,615
* ushort - 16-bit unsigned integer - 0 to 65,535
* enum -
* struct
* object - base class for all data types,
- object obj; - value type if converted to an object type it is called boxing
- obj = 100; - object is converted to a value type it is called unboxing
* dynamic - store any type of value
* string - allows you to assign any string values to a variable
- quoted - String str = "quoted string"
- @quoted - @"quoted string"
* type* -
* char* -
* int* -
Modifiers
abstract
async
const
event
extern
new
override
partial
readonly
sealed
static
unsafe
virtual
volatile
╔═══════════════════╗
║ ARRAYS ║
╚═══════════════════╝
int[] nums = { 1, 2, 3, 4, 5 };
string[] fruits = {"apple","banana","orange"};
// Empty Array
int[] nums = new int[10];
// Multidimensional Array
int[,] matrix = new int[2,2];
matrix[0,0] = 1;
matrix[0,1] = 2;
matrix[1,0] = 3;
matrix[1,1] = 4;
int[,] predefinedMatrix = new int[2,2] { { 1, 2 }, { 3, 4 } };
╔═══════════════════╗
║ LISTS ║
╚═══════════════════╝
The difference between a list and an array is that lists are dynamic sized, while arrays have a fixed size. When you do not know the amount of variables your array should hold, use a list instead.
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
// Add entire array to a list
List<int> numbers = new List<int>();
int[] array = new int[] { 1, 2, 3 };
numbers.AddRange(array);
List<string> fruits = new List<string>();
// add fruits
fruits.Add("apple");
fruits.Add("banana");
fruits.Add("orange");
// now remove the banana
fruits.Remove("banana");
Console.WriteLine(fruits.Count);
We can also use RemoveAt to specify an index of an item to remove. In our case, to remove the banana, we will use the index 1.
List<string> fruits = new List<string>();
// add fruits
fruits.Add("apple");
fruits.Add("banana");
fruits.Add("orange");
// now remove the banana
fruits.RemoveAt(1);
Console.WriteLine(fruits.Count);
We can use AddRange to join between lists.
List<string> food = new List<string>();
food.Add("apple");
food.Add("banana");
List<string> vegetables = new List<string>();
vegetables.Add("tomato");
vegetables.Add("carrot");
food.AddRange(vegetables);
Console.WriteLine(food.Count);
using System;
using System.Collections.Generic;
public class Hello
{
public static void Main()
{
// TODO: add your code here
List<int> primeNumbers = new List<int>();
primeNumbers.Add(2);
primeNumbers.Add(3);
primeNumbers.Add(5);
primeNumbers.Add(7);
primeNumbers.Add(11);
// test code
}
}
╔═══════════════════╗
║ DICTIONARIES ║
╚═══════════════════╝
Dictionary<string, long> phonebook = new Dictionary<string, long>();
phonebook.Add("Alex", 4154346543);
phonebook["Jessica"] = 4159484588;
using System;
using System.Collections.Generic;
public class Hello
{
public static void Main()
{
// TODO: add the inventory dictionary here
Dictionary<string, int> inventory = new Dictionary<string, int>();
inventory.Add("apple", 3);
inventory.Add("orange", 5);
inventory.Add("banana", 2);
Console.WriteLine(inventory["apple"]);
Console.WriteLine(inventory["orange"]);
Console.WriteLine(inventory["banana"]);
inventory.Remove("apple"); // remove item by dictionary name
}
}
╔═══════════════════╗
║ FORMATTING ║
╚═══════════════════╝
int x = 1, y = 2;
int sum = x + y;
string sumCalculation = String.Format("{0} + {1} = {2}", x, y, sum);
Console.WriteLine(sumCalculation);
using System;
public class Hello
{
public static void Main()
{
string firstName = "John";
string lastName = "Doe";
int age = 27;
string sentence = String.Format("{0} {1} is {2} years old.", firstName, lastName, age);
Console.WriteLine(sentence);
}
}
using System;
public class Hello
{
public static void Main()
{
string firstName = "John";
string lastName = "Doe";
int age = 27;
// TODO: change this
string sentence = $"{firstName} {lastName} is {age} years old.";
Console.WriteLine(sentence);
}
}
╔═══════════════════╗
║ SYSTEM METHODS ║
╚═══════════════════╝
* Console.WriteLine() - writes to the console, if it is a string, wrap in quotes, or you can pass through a variables
the parentheses after the WriteLine method. This is the way to pass data, or arguments, to methods. In our case WriteLine is the method and we pass "Hello World!" to it as an argument. String arguments must be enclosed in quotation marks.
- Console.WriteLine("Hello, world!");
* Console.ReadLine() - waits for a user input and then assigns to a variable
* Convert.To() - will convert a variable to a different type
- Convert.ToDouble, Convert.ToString, Convert.ToInt32
- Convert.ToInt32(Console.ReadLine());
* sizeof() - expression that yields the storage size of the object of type in bytes
** CONVERSION METHODS **
- ToBoolean - converts a type to a boolean value, where possible
- ToByte - converts a type to a byte
- ToChar - converts a type to a single unicode character, where possible
- ToDateTime - converts a type(integer or string type) to date-time structures
- ToDecimal - converts a floating point or integer type to a decimal type
- ToDouble - converts a type to a double
- ToInt16 - converts a type to a 16-bit integer
- ToInt32 - converts a type to a 32-bit integer
- ToInt64 - converts a type to 65-bit integer
- ToSbyte - converts a type to a signed byte type
- ToSingle - converts a type to a small floating point
- ToString - converts a type to string
- ToType - converts a type to a specific type
- ToUInt16 - converts a type to an unsigned int type
- ToUInt32 - converts a type to an unsigned long type
- ToUInt64 - converts a type to an unsigned big integer
╔═══════════════════╗
║ LOGICAL OPERATORS ║
╚═══════════════════╝
&& - and
|| - or
! - not
? - The ?: operator works the following way: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.
int age = 42;
string msg;
msg = (age >= 18) ? "Welcome" : "Sorry";
Console.WriteLine(msg);
╔═══════════════════╗
║ CONDITIONALS ║
╚═══════════════════╝
** IF / ELSE **
if (condition)
{
statement
}
else
{
statement
}
** ELSE IF **
int x = 33;
if (x == 8) {
Console.WriteLine("Value of x is 8");
}
else if (x == 18) {
Console.WriteLine("Value of x is 18");
}
else if (x == 33) {
Console.WriteLine("Value of x is 33");
}
else {
Console.WriteLine("No match");
}
//Outputs "Value of x is 33"
** NESTED **
int mark = 100;
if (mark >= 50) {
Console.WriteLine("You passed.");
if (mark == 100) {
Console.WriteLine("Perfect!");
}
}
else {
Console.WriteLine("You failed.");
}
** SWITCH **
int num = 3;
switch (num)
{
case 1:
Console.WriteLine("one");
break;
case 2:
Console.WriteLine("two");
break;
case 3:
Console.WriteLine("three");
break;
}
//Outputs "three"
int x = 33;
switch(x) {
case 8:
Console.WriteLine("Value is 8");
break;
case 18:
Console.WriteLine("Value is 18");
break;
case 33:
Console.WriteLine("Value is 33");
break;
}
//Output: Value is 33
int age = 88;
switch (age) {
case 16:
Console.WriteLine("Too young");
break;
case 42:
Console.WriteLine("Adult");
break;
case 70:
Console.WriteLine("Senior");
break;
default:
Console.WriteLine("The default case");
break;
}
// Outputs "The default case"
╔═══════════════════╗
║ FOR LOOPS ║
╚═══════════════════╝
for( [variable to count iterations] ; [conditions checked for] ; [code to execute every loop])
int i;
for( i = 0; i < 10; i++)
OR
for( int i = 0; i < 10; i++)
public class Functions
{
public static void Main()
{
string x = "Hi";
// write for loop here
for(int n = 0; n < 10; n++)
{
Console.WriteLine(x);
}
string x = "Hi";
int n = 10;
for( int i = 0; i < n; i++)
{
Console.WriteLine(x);
}
}
}
╔═══════════════════╗
║ WHILE LOOPS ║
╚═══════════════════╝
while([conditions to be checked])
{
[Code to execute]
}
int n = 0;
while( n == 0)
{
Console.WriteLine("N is 0");
n++;
}
public class Functions
{
public static void Main()
{
string x = "Hi";
int n = 10;
int i = 0;
//write while loop here
while(i < n)
{
Console.WriteLine(x);
i++;
}
}
}
╔═══════════════════╗
║ METHODS ║
╚═══════════════════╝
[Modifiers (E.G public or static)] [Type of output] [Name] ( [parameter 1],[parameter 2] ...)
{
}
using System;
public class Methods
{
public static void Main()
{
int x = 2;
int y = 2;
int a = foo(x, y);
Console.WriteLine(a);
}
//write method foo here
public static int foo(int x, int y)
{
return x / y;
}
}
╔═══════════════════╗
║ CLASSES ║
╚═══════════════════╝
using System;
class car{
public int numTires = 4;
public int year = 2000;
public bool runs = true;
}
public class MainClass{
public static void Main(){
car car1 = new car();
car car2 = new car();
car car3 = new car();
// Test code
Console.WriteLine(car1.numTires);
Console.WriteLine(car2.year);
Console.WriteLine(car3.runs);
}
}
public class MainClass
{
class Vehicle
{ // Class properties
public string Type;
public int NumTires;
public int Year;
public bool Runs;
// Init class properties
public Vehicle(string type, int numTires, int year, bool runs)
{
Type = type;
NumTires = numTires;
Year = year;
Runs = runs;
}
}
public static void Main()
{
//Create vehicles here
Vehicle car = new Vehicle("car", 4, 2000, true);
Vehicle oldcar = new Vehicle("oldcar", 4, 1980, false);
Vehicle bike = new Vehicle("bike", 2, 2017, true);
// Test code
Console.WriteLine(car.Type);
Console.WriteLine(oldcar.Runs);
Console.WriteLine(bike.NumTires);
}
}