forked from tkonk46/KeywordExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (124 loc) · 4.15 KB
/
Program.cs
File metadata and controls
167 lines (124 loc) · 4.15 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KeywordExamples
{
class Program
{
public static readonly string ERROR_MESSAGE = "You got an error";
public const string BETTER_ERROR_MESSAGE = "You have an error"; //Better way to hard-code strings!
static void Main(string[] args)
{
int a = 10;
for (int i = 0; i < 10; i++)
{
a = MethodDemonstratingFinally(a);
Console.WriteLine("variable a is currently {0}", a);
}
//Console.ReadLine();
InternalExample.Class2.CallClass1(); //I can call class 2 because it is public
//InternalExample.Class1 //Can't create an internal class from another assembly. It's encapsulated.
//ErrorMessage = "You have an error"; //Doesn't work, it's read-only
//BETTER_ERROR_MESSAGE = "Sir, you have an error!"; //Doesn't work, constants are locked in!
//TypeCastingExamples();
ClassWithProtectedBase cwpb = new ClassWithProtectedBase();
cwpb.SaySalary();
int number = 100;
MultiplyBy10(number);
Console.WriteLine(number);
MyObject o = new MyObject();
o.MyNumber = 1;
MultiplyBy10(o);
Console.WriteLine(o);
Console.ReadLine();
}
public static void JerrySaysHello()
{
Console.WriteLine("Hello from Jerry");
}
public static void MultiplyBy10(int number)
{
number = number * 10;
}
public static void MultiplyBy10(MyObject obj)
{
obj.MyNumber = obj.MyNumber * 10;
}
private static void TypeCastingExamples()
{
object u = new UninheritableClass();
Type t = u.GetType();
if (typeof(UninheritableClass) == t)
{
Console.WriteLine("u is an uninheritable class");
}
//byte[] fileBytes = System.IO.File.ReadAllBytes("C:\\Users\\joewe\\Desktop\\week1classes.dll");
if (u is UninheritableClass) //I'm using "IS" to examine Type information and test that my variable is an object of a given type.
{
((UninheritableClass)u).SayHello();
}
u = "Hello";
//((UninheritableClass)u).SayHello(); //OH NO, Runtime Exception
UninheritableClass uc = u as UninheritableClass; //Preferred way to do this! Test and TypeCast in one step!
if (uc != null)
{
uc.SayHello();
}
}
public static int MethodDemonstratingFinally(int a)
{
Random r = new Random();
int randomNumber = r.Next(0, 2);
try
{
if (randomNumber == 1)
throw new Exception("I'm forcing an exception to be thrown randomly");
//a++;
}
catch(OutOfMemoryException)
{
Console.WriteLine("We ran out of memory");
}
catch(Exception ex)
{
Console.WriteLine(ex);
//a++; //Moved this to the finally block
}
finally
{
a++;
}
return a;
}
}
public class MyObject
{
public int MyNumber;
}
//public class MyNewClass : UninheritableClass //Doesn't work!
//{
//}
public sealed class UninheritableClass
{
public UninheritableClass()
{
}
public void SayHello()
{
Console.WriteLine("Hi, everybody!");
}
}
public class ClassWithProtectedBase : ProtectedBaseClass
{
public void SaySalary()
{
Console.WriteLine(SalaryInformation);
}
}
public class ProtectedBaseClass
{
protected decimal SalaryInformation = 100;
}
}