-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
174 lines (151 loc) · 5.18 KB
/
Program.cs
File metadata and controls
174 lines (151 loc) · 5.18 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
using System;
using System.Collections.Generic;
namespace C__6._0
{
class Program
{
static void Main(string[] args)
{
/*
stringInterpolationExample();
nullConditionalOperatorExample();
nameOfOperatorExample();
exceptionFilterExample();
indexInitializer();
usingStatic();
autoPropertyInitializer();
*/
}
// 1.1 String Interpolation.
private static void stringInterpolationExample()
{
displayTitle("String interpolation example");
string t = "random variable value";
Console.WriteLine($"Display variable value: {t}");
Person p = new Person();
p.name = "tony";
Console.WriteLine($"Display name value: {p.name}");
}
// 1.2 Null conditional operator example.
private static void nullConditionalOperatorExample()
{
displayTitle("Null conditional operator");
Person p = new Person();
Console.WriteLine($"Display name value: {p?.name ?? "Null Name"}");
p.name = "John";
Console.WriteLine($"Display name value: {p?.name ?? "Null Name"}");
Console.WriteLine($"Display address value: {p?.address ?? "Null Address"}");
p.address = "Street 123";
Console.WriteLine($"Display address value: {p?.address ?? "Null Address"}");
}
// 1.3 Name of operator example.
private static void nameOfOperatorExample()
{
displayTitle("nameof operator example");
Console.WriteLine($"Name of Class:{nameof(Person)}");
Console.WriteLine($"Name of Attribute:{nameof(Person.name)}");
}
// 1.4 Expression-bodied members example.
private static void displayTitle(string title) => Console.WriteLine($"\n{title} \n");
// 1.5 Exception filters example
private static void exceptionFilterExample()
{
int x = 54;
int y = 0;
try
{
int result = x / y;
}
catch (Exception ex) when (ex.Message.Contains("divide"))
{
Console.WriteLine($"Message2: {ex.Message}");
}
}
// 1.6 Index intializer
private static void indexInitializer()
{
Dictionary<int, string> intializedList = new Dictionary<int, string>
{
[1] = "Index one",
[2] = "Index two",
[3] = "Index three"
};
Console.WriteLine($"Index 1: {intializedList[1]}");
Console.WriteLine($"Index 2: {intializedList[2]}");
Console.WriteLine($"Index 3: {intializedList[3]}");
}
// 1.7 Using static
private static void usingStatic()
{
int a = 10;
int b = 20;
Console.WriteLine($"Sum a + b static {Calculator.sumStatic(a, b)}");
Console.WriteLine($"Sum a + b not static {new Calculator().sumNotStatic(a, b)}");
}
// 1.8 Auto property initializer
private static void autoPropertyInitializer()
{
InitializerExample init = new InitializerExample();
Console.WriteLine($"Default age: {init.age}");
Console.WriteLine($"Default address: {init.address}");
}
// 1.9 Overload method
private static void overloadMethod(string v)
{
Console.WriteLine($"Display string: {v}");
}
// 1.9 Overload method
private static void overloadMethod(int v)
{
Console.WriteLine($"Display int: {v}");
}
// 1.9 Overload method
private static void overloadMethod(object v)
{
Console.WriteLine($"Display object: {v}");
}
// 1.9 Overload method
private static void overloadMethod(bool v)
{
Console.WriteLine($"Display boolean: {v}");
}
// 1.9 Overload method
private static void overloadMethod(Person v)
{
Console.WriteLine($"Display person: {v}");
}
// 1.9 Overload method
private static void overloadMethodTest()
{
overloadMethod(10);
overloadMethod(new Person());
overloadMethod(true);
overloadMethod(new object());
overloadMethod(new System.Text.StringBuilder());
}
// 2.0 Read only auto properties
private static void ReaOnlyAutoProperties()
{
Console.WriteLine($"Read only accessor: {new ReadOnlyExample().age}");
}
}
public class Person
{
public string name { get; set; }
public string address { get; set; }
}
public class Calculator
{
public static int sumStatic(int a, int b) => a + b;
public int sumNotStatic(int a, int b) => a + b;
}
public class InitializerExample
{
public int age { get; set; } = 10;
public string address { get; set; } = "Default Address";
}
public class ReadOnlyExample
{
public int age { get; } = 27;
}
}