-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
459 lines (366 loc) · 12.4 KB
/
Program.cs
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
// See https://aka.ms/new-console-template for more information
using _30DayChallenge.Net.Day1;
using _30DayChallenge.Net.Day2;
using _30DayChallenge.Net.Day3;
using _30DayChallenge.Net.Day4;
using _30DayChallenge.Net.Day5;
using _30DayChallenge.Net.Day6;
using _30DayChallenge.Net.Day7;
using _30DayChallenge.Net.Day8;
using _30DayChallenge.Net.Day9;
using _30DayChallenge.Net.Day10;
using _30DayChallenge.Net.Day11;
using _30DayChallenge.Net.Day13;
using _30DayChallenge.Net.Day14;
using _30DayChallenge.Net.Day15;
using _30DayChallenge.Net.Day16;
using _30DayChallenge.Net.Day17;
using _30DayChallenge.Net.Day18;
using _30DayChallenge.Net.Day19;
using System.Drawing;
using _30DayChallenge.Net;
using _30DayChallenge.Net.Day20;
using _30DayChallenge.Net.Day21;
using _30DayChallenge.Net.Day22;
using _30DayChallenge.Net.Day23;
using _30DayChallenge.Net.Day24;
using _30DayChallenge.Net.Day25;
using _30DayChallenge.Net.Day26;
using _30DayChallenge.Net.Day27;
using _30DayChallenge.Net.Day28;
using _30DayChallenge.Net.Day29;
using _30DayChallenge.Net.Day30;
Console.WriteLine("S.No.\t30 Day .Net Challenge\tTutorial\r\nDay 01\tBoolean Expressions\t\r\nDay 02\tVariable Scope & Logic Control\t\r\nDay 03\tSwitch Constructs\t\r\nDay 04\tFor Loops\t\r\nDay 05\tWhile & do-while\t\r\nDay 06\tString built-in methods\t\r\nDay 07\tString built-in methods Part 2\t\r\nDay 08\tException Handling\t\r\nDay 09\tNull Safety\t\r\nDay 10\tFile Paths\t\r\nDay 11\tHelper Methods - Array\t\r\nDay 12\tAzure Functions\t\r\nDay 13\tConfigureAwait(false)\t\r\nDay 14\tLimit Concurrent Async Operations\t\r\nDay 15\tLazy Initialization\t");
Console.WriteLine("Enter Day value in integer");
int dayValue = Convert.ToInt32(Console.ReadLine());
ChooseFrom30DayChallenge(dayValue);
#region SwitchDay to be executed
static string ChooseFrom30DayChallenge(int value)
{
return value switch
{
(int)_30DayChallengeEnum.Day1 => ExecuteDay1(),
(int)_30DayChallengeEnum.Day2 => ExecuteDay2(),
(int)_30DayChallengeEnum.Day3 => ExecuteDay3(),
(int)_30DayChallengeEnum.Day4 => ExecuteDay4(),
(int)_30DayChallengeEnum.Day5 => ExecuteDay5(),
(int)_30DayChallengeEnum.Day6 => ExecuteDay6(),
(int)_30DayChallengeEnum.Day7 => ExecuteDay7(),
(int)_30DayChallengeEnum.Day8 => ExecuteDay8(),
(int)_30DayChallengeEnum.Day9 => ExecuteDay9(),
(int)_30DayChallengeEnum.Day10 => ExecuteDay10(),
(int)_30DayChallengeEnum.Day11 => ExecuteDay11(),
(int)_30DayChallengeEnum.Day12 => ExecuteDay12(),
(int)_30DayChallengeEnum.Day13 => ExecuteDay13().Result,
(int)_30DayChallengeEnum.Day14 => ExecuteDay14(),
(int)_30DayChallengeEnum.Day15 => ExecuteDay15(),
(int)_30DayChallengeEnum.Day16 => ExecuteDay16(),
(int)_30DayChallengeEnum.Day17 => ExecuteDay17(),
(int)_30DayChallengeEnum.Day18 => ExecuteDay18(),
(int)_30DayChallengeEnum.Day19 => ExecuteDay19(),
(int)_30DayChallengeEnum.Day20 => ExecuteDay20().Result,
(int)_30DayChallengeEnum.Day21 => ExecuteDay21(),
(int)_30DayChallengeEnum.Day22 => ExecuteDay22(),
(int)_30DayChallengeEnum.Day23 => ExecuteDay23(),
(int)_30DayChallengeEnum.Day24 => ExecuteDay24(),
(int)_30DayChallengeEnum.Day25 => ExecuteDay25(),
(int)_30DayChallengeEnum.Day26 => ExecuteDay26(),
(int)_30DayChallengeEnum.Day27 => ExecuteDay27(),
(int)_30DayChallengeEnum.Day28 => ExecuteDay28(),
(int)_30DayChallengeEnum.Day29 => ExecuteDay29(),
(int)_30DayChallengeEnum.Day30 => ExecuteDay30(),
_ => "invalid day"
};
}
#endregion
#region Day 1 - Expressions
static string ExecuteDay1()
{
Expressions.CheckEqualityOperator();
Expressions.CheckEqualityBuiltInMethods();
Expressions.CheckInEqualityOperator();
Expressions.CheckComparisonOperator();
Expressions.CheckBooleanMethods();
return "Executed Day 1 successfully..!!";
}
#endregion
#region Day 2 - Variable Scope & Logic Control with Code Blocks
static string ExecuteDay2()
{
CodeBlocksAndScope.VariableInCodeBlock();
CodeBlocksAndScope.VariableOutCodeBlock();
CodeBlocksAndScope.VariableAboveCodeBlock();
CodeBlocksAndScope.VariableAboveCodeBlockv1();
return "Executed Day 2 successfully..!!";
}
#endregion
#region Day 3 - Switch Constructs
static string ExecuteDay3()
{
Switch.SwitchExample();
Switch.ChangeSwitchLabelExample();
Switch.MultipleSwitchLabelExample();
return "Executed Day 3 successfully..!!";
}
#endregion
#region Day 4 - For Loops
static string ExecuteDay4()
{
ForLoops.ForLoopExample();
ForLoops.BackwardForLoopExample();
ForLoops.IterationForLoopExample();
ForLoops.BreakForLoopExample();
return "Executed Day 4 successfully..!!";
}
#endregion
#region Day 5 - While & do-while
static string ExecuteDay5()
{
WhileLoop.DoWhileLoopExample();
WhileLoop.WhileLoopExample();
WhileLoop.ContinueDoWhileLoopExample();
return "Executed Day 5 successfully..!!";
}
#endregion
#region Day 6 - String built-in methods
static string ExecuteDay6()
{
StringMethods.IndexOfExample();
StringMethods.SubstringExample();
StringMethods.SubstringExample2();
return "Executed Day 6 successfully..!!";
}
#endregion
#region Day 7 - String built-in methods Part 2
static string ExecuteDay7()
{
StringMethodsPart2.IndexOfAnyMethod();
StringMethodsPart2.LastIndexOfMethod();
return "Executed Day 7 successfully..!!";
}
#endregion
#region Day 8 - Exception Handling
static string ExecuteDay8()
{
ExceptionHandling.SimpleExceptionBlock();
ExceptionHandling.NestedExceptionBlock();
return "Executed Day 8 successfully..!!";
}
#endregion
#region Day 9 - Null Safety
static string ExecuteDay9()
{
NullSafety.Unsafe();
NullSafety.SafeExample();
return "Executed Day 9 successfully..!!";
}
#endregion
#region Day 10 - File Path
static string ExecuteDay10()
{
FilePath.DisplayCurrentDirectory();
FilePath.DisplaySpecialDirectory();
FilePath.DisplayOSPathCharacters();
FilePath.DisplayFileExtension();
return "Executed Day 10 successfully..!!";
}
#endregion
#region Day 11 - Helper Methods - Array
static string ExecuteDay11()
{
ArrayHelperMethods.SortExample();
ArrayHelperMethods.ReverseSortExample();
ArrayHelperMethods.ClearExample();
ArrayHelperMethods.ResizeAndAdd();
return "Executed Day 11 successfully..!!";
}
#endregion
#region Day 12 - Azure Functions
static string ExecuteDay12()
{
// Set the Azure Function Example as startup project and run locally.
// The class created under Day 12 folder is for demonstration purposes, cannot be invoked from console application.
// Follow the tutorial :
return "Executed Day 12 successfully..!!";
}
#endregion
#region Day 13: ConfigureAwait(false)
static async Task<string> ExecuteDay13()
{
await ConfigureAwait.OldApproach();
ConfigureAwait.OptimizedApproachAsync().ConfigureAwait(false);
return "Executed Day 13 successfully..!!";
}
#endregion
#region Day 14: Limit Concurrency
static string ExecuteDay14()
{
// The idea is to demonstrate the concept, no output for simplicity
LimitConcurrency.OldApproach(null).ConfigureAwait(false);
LimitConcurrency.OptimizedApproachAsync(null).ConfigureAwait(false);
return "Executed Day 14 successfully..!!";
}
#endregion
#region Day 15: Lazy Initialization
static string ExecuteDay15()
{
Console.WriteLine("Application started.");
Console.WriteLine("Press any key to use the expensive resource...");
Console.ReadKey();
// Accessing the Value property of _lazyExpensiveResource for the first time triggers the initialization.
new Lazy<ExpensiveObject>().Value.UseResource();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
return "Executed Day 15 successfully..!!";
}
#endregion
#region Day 16: In-Memory Cache
static string ExecuteDay16()
{
Console.WriteLine("Fetching product with ID 1 for the first time:");
var product = InMemoryCache.GetProductById(1);
Console.WriteLine($"Product Name: {product.Name}\n");
Console.WriteLine("Fetching product with ID 1 again:");
product = InMemoryCache.GetProductById(1); // This time, it should come from the cache
Console.WriteLine($"Product Name: {product.Name}\n");
return "Executed Day 16 successfully..!!";
}
#endregion
#region Day 17: Increment Class
static string ExecuteDay17()
{
IncrementClass.TestIncrementCounter();
return "Executed Day 17 successfully..!!";
}
#endregion
#region Day 18: AggressiveInlining Attribute
static string ExecuteDay18()
{
AggressiveInlining.MultiplyByTwo(10);
AggressiveInlining.MultiplyByTwoWithAggressiveInlining(10);
return "Executed Day 18 successfully..!!";
}
#endregion
#region Day 19: Stack vs. Heap Allocation
static string ExecuteDay19()
{
StackVsHeap.InefficientMethod(0);
StackVsHeap.EfficientMethod(0);
return "Executed Day 19 successfully..!!";
}
#endregion
#region Day 20: Task vs. Value Task
static async Task<string> ExecuteDay20()
{
Console.WriteLine("Fetching data with Task...");
string result = await TaskVsValueTask.FetchDataAsync();
Console.WriteLine(result);
Console.WriteLine("Fetching data with ValueTask...");
string resultValueTask = await TaskVsValueTask.FetchDataValueTaskAsync();
Console.WriteLine(resultValueTask);
return "Executed Day 20 successfully..!!";
}
#endregion
#region Day 21: String Comparisons
static string ExecuteDay21()
{
StringComparisons.BadMethod();
StringComparisons.GoodMethod();
return "Executed Day 21 successfully..!!";
}
#endregion
#region Day 22: Array Pool
static string ExecuteDay22()
{
Console.WriteLine("Demonstrating BAD Method:");
ArrayPoolExample.BadMethod();
Console.WriteLine("\nDemonstrating GOOD Method:");
ArrayPoolExample.GoodMethod();
return "Executed Day 22 successfully..!!";
}
#endregion
#region Day 23: Span Over Arrays
static string ExecuteDay23()
{
byte[] largeData = new byte[100]; // Simulate a large data set
Random rng = new Random();
rng.NextBytes(largeData); // Populate with random bytes
// Process using array slice
byte[] slice = new byte[10]; // Creating a new array for the slice
Array.Copy(largeData, 10, slice, 0, 10); // Copying data
SpanOverArray.ProcessData(slice);
// Process using Span<T>
Span<byte> span = largeData.AsSpan(10, 10); // Creating a span starting at index 10
SpanOverArray.ProcessData(span);
return "Executed Day 23 successfully..!!";
}
#endregion
#region Day 24: Avoid Exceptions in Flow Control
static string ExecuteDay24()
{
Console.WriteLine("Enter a number:");
string input = Console.ReadLine();
AvoidExceptions.BadWay(input);
AvoidExceptions.GoodWay(input);
return "Executed Day 24 successfully..!!";
}
#endregion
#region Day 25: Use Exception Filters
static string ExecuteDay25()
{
// Using conventional exception handling
// This will cause ArgumentNullException
ExceptionFilters.MultipleCatch(null);
// Reset input for valid processing
ExceptionFilters.GoodWay("Valid input");
// This input will cause InvalidOperationException
ExceptionFilters.GoodWay("invalid");
return "Executed Day 25 successfully..!!";
}
#endregion
#region Day 26: Loop Unrolling
static string ExecuteDay26()
{
LoopUnrolling.BadWay();
LoopUnrolling.GoodWay();
return "Executed Day 26 successfully..!!";
}
#endregion
#region Day 27: Query vs Method Syntax
static string ExecuteDay27()
{
QueryVsMethod.MethodSyntax();
QueryVsMethod.QuerySyntax();
return "Executed Day 27 successfully..!!";
}
#endregion
#region Day 28: Use Stackalloc
static string ExecuteDay28()
{
StackAlloc.BadWay();
StackAlloc.GoodWay();
return "Executed Day 28 successfully..!!";
}
#endregion
#region Day 29: Generics & Custom Interfaces
static string ExecuteDay29()
{
GenericCustomInterfaces.Example();
return "Executed Day 29 successfully..!!";
}
#endregion
#region Day 30: Efficient Serialization
static string ExecuteDay30()
{
Person person = new Person { Name = "John Doe", Age = 30 };
// XML Serialization
string xmlData = EfficientSerialization.XML(person);
Console.WriteLine("XML Serialization Output:");
Console.WriteLine(xmlData);
// JSON Serialization
string jsonData = EfficientSerialization.JSON(person);
Console.WriteLine("JSON Serialization Output:");
Console.WriteLine(jsonData);
return "Executed Day 30 successfully..!!";
}
#endregion