-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperators.cs
187 lines (110 loc) · 3.93 KB
/
Operators.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
namespace CSharp.basics;
public class Operators
{
// ▬ (1) The "Modulus Operator" ("Remainder") ("%") ▬
public static void Modulus()
{
Console.WriteLine("Modulus Operator: " + (5 % 4));
}
// ▬ (2-1) The "Increment Operator" ("++") ▬
public static void Increment()
{
int number = 9;
number++;
Console.WriteLine("Increment Operator: " + number);
}
// ▬ (2-2) The "InDecrement Operator" ("--") ▬
public static void Decrement()
{
int number = 9;
number--;
Console.WriteLine("Decrement Operator: " + number);
}
// ▬ (3) The "Is Operator" ("is") ▬
public static void IsOfType()
{
bool a = "abc" is string;
bool b = "123" is int;
Console.WriteLine("Is Operator for String: " + a);
Console.WriteLine("Is Operator for Int: " + b);
}
// ▬ (4) The "As Operator" ("as") ▬
public static void AsTypeCasting()
{
string s1 = "A string..";
object obj1 = s1;
string s2 = obj1 as string; // ◄ "Type Casting"
Console.WriteLine("As Operator for Type Casting: " + s2);
}
// ▬ (5) The "Ternary/Conditional Operator" ("Condition ? True : False") ▬
public static void TernaryOrConditional()
{
int x = 10;
int value = 5;
// "Ternary Operator" = (Condition) ? True : False
bool isGreater = (x > value) ? true : false; // ◄ "var" → indicates "Any Data Type" for the "Variable"
// ▼ "Replace" this "Form": ▼
// if(x e) {
// isGreater = true;
// }
// else
// {
// isGreater = false;
// }
Console.WriteLine("Ternary/Conditional Operator: " + isGreater);
}
// ▬ (6) The "Null-Coalescing Operator" (True ?? False") ▬
public static void NullCoalescing()
{
object obj1 = null;
// ▼ "Null-Coalescing Operator" (True ?? False") ▼
object obj2 = obj1 ?? new object();
Console.WriteLine("Null-Coalescing Operator: " + obj2);
}
// ▬ (7) The "sizeof Operator" → that "Provides" the "Size" in "Bytes" ▬
public static void SizeOf()
{
// ▼ Te "Size" of "Data Type" ▼
Console.WriteLine("Size Of Operator: " + sizeof(char));
}
// ▬ (8) The "typeof Operator" → that "Provides" the "System.Type" Object for a "Data Type" ▬
public static void TypeOf()
{
// ▼ The "Size" of "Data Type" ▼
Console.WriteLine("Type Of Operator: " + typeof(int));
}
}
// ▬ The "Box" Class with the "Overloading Operator" ▬
class Box {
// ▼ "Properties" ("Class Members Variables") ▼
private int length;
private int width;
private int height;
// ▬ "Constructor" with "Arguments" ▼
public Box(int length, int width, int height){
// ►► this.Property = Constructor_Argument ◄◄
this.length = length;
this.width = width;
this.height = height;
}
// ▬ "Getter" → for "Length" Private Property ▬
private int GetLength(){
return length;
}
// ▬ "Getter" → for "Width" Private Property ▬
private int GetWidth(){
return width;
}
// ▬ "Getter" → for "Height" Private Property ▬
private int GetHeight(){
return height;
}
// ▬ (9) The "Overloading Operator" (operator +Symbol) to "Add 2 Box" Objects ▬
public static Box operator +(Box box1, Box box2) {
return new Box(
box1.GetLength() + box2.GetLength(),
box1.GetWidth() + box2.GetWidth(),
box1.GetHeight() + box2.GetHeight()
);
}
}