-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter17.cs
More file actions
252 lines (206 loc) · 6.96 KB
/
Chapter17.cs
File metadata and controls
252 lines (206 loc) · 6.96 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
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
using System;
namespace Chapter17
{
class MyStack<T>
{
T[] StackArray;
int StackPointer = 0;
public void Push(T x)
{
if(!IsStackFull)
StackArray[StackPointer++] = x;
}
public T Pop()
{
return (!IsStackEmpty)
? StackArray[--StackPointer]
: StackArray[0];
}
const int MaxStack = 10;
bool IsStackFull {get{ return StackPointer >= MaxStack;}}
bool IsStackEmpty {get{ return StackPointer <= 0;}}
public MyStack()
{
StackArray = new T[MaxStack];
}
public void Print()
{
for(int i = StackPointer-1; i >= 0; i--)
{
Console.WriteLine(" Value: {0}", StackArray[i]);
}
}
}
class Simple
{
static public void ReverseAndPrint<T>(T[] arr)
{
Array.Reverse(arr);
foreach(T item in arr)
Console.Write("{0}, ", item.ToString());
Console.WriteLine("");
}
}
class Holder<T>
{
T[] Vals = new T[3];
public Holder(T v0, T v1, T v2)
{Vals[0] = v0; Vals[1] = v1; Vals[2] = v2;}
public T[] GetValues() {return Vals;}
}
static class ExtendHolder
{
public static void Print<T>(this Holder<T> h)
{
T[] vals = h.GetValues();
Console.WriteLine("{0},\t{1},\t{2}", vals[0], vals[1], vals[2]);
}
}
struct PieceOfData<T>
{
public PieceOfData(T value){_data = value;}
private T _data;
public T Data
{
get{return _data;}
set{_data = value;}
}
}
delegate void MyDelegate<T>(T value);
class Simple1
{
static public void PrintString(string s)
{
Console.WriteLine(s);
}
static public void PrintUpperString(string s)
{
Console.WriteLine("{0}", s.ToUpper());
}
}
delegate TR Func<T1, T2, TR>(T1 p1, T2 p2);
class Simple2
{
static public string PrintString(int p1, int p2)
{
int total = p1 + p2;
return total.ToString();
}
}
interface IMyIfc<T>
{
T ReturnIt(T inValue);
}
class Simple3<S>:IMyIfc<S>
{
public S ReturnIt(S inValue)
{ return inValue;}
}
class Simple4 : IMyIfc<int>, IMyIfc<string>
{
public int ReturnIt(int inValue)
{return inValue;}
public string ReturnIt(string inValue)
{
return inValue;
}
}
class Animal
{
public int NumberOfLegs = 4;
}
class Dog : Animal
{
}
delegate T Factory<out T> ();
interface IMyIfc1<out T>
{
T GetFirst();
}
class SimpleReturn<T> : IMyIfc1<T>
{
public T[] items = new T[2];
public T GetFirst() {return items[0];}
}
class Program
{
static Dog MakeDog()
{
return new Dog();
}
delegate void Action1<in T>(T a);
static void ActionOnAnimal(Animal a){Console.WriteLine(a.NumberOfLegs);}
static void DoSomething(IMyIfc1<Animal> returner)
{
Console.WriteLine(returner.GetFirst().NumberOfLegs);
}
static void MainTest()
{
Console.WriteLine("Alex 2018-4-16");
Console.WriteLine("*************************************");
MyStack<int> StackInt = new MyStack<int>();
MyStack<String> StackString = new MyStack<string>();
StackInt.Push(3);
StackInt.Push(5);
StackInt.Push(7);
StackInt.Push(9);
StackInt.Print();
StackString.Push("This is fun");
StackString.Push("Hi there!");
StackString.Print();
Console.WriteLine("**************************************");
var intArray = new int[] {3,5,7,9,11};
var stringArray = new string[] {"first", "second", "third"};
var doubleArray = new double[] {3.567, 7.891, 2.345};
Simple.ReverseAndPrint<int>(intArray);
Simple.ReverseAndPrint(intArray);
Simple.ReverseAndPrint<string>(stringArray);
Simple.ReverseAndPrint(stringArray);
Simple.ReverseAndPrint<double>(doubleArray);
Simple.ReverseAndPrint(doubleArray);
Console.WriteLine("***************************************");
var intHolder = new Holder<int>(3,5,7);
var stringHolder = new Holder<string>("a1", "b2", "c3");
intHolder.Print();
stringHolder.Print();
Console.WriteLine("***************************************");
var intData = new PieceOfData<int>(10);
var stringData = new PieceOfData<string>("Hi there.");
Console.WriteLine("intData = {0}", intData.Data);
Console.WriteLine("stringData = {0}", stringData.Data);
Console.WriteLine("***************************************");
var MyDel = new MyDelegate<string>(Simple1.PrintString);
MyDel += Simple1.PrintUpperString;
MyDel("Hi, there!");
Console.WriteLine("****************************************");
var MyDel1 = new Func<int, int, string>(Simple2.PrintString);
Console.WriteLine("Total: {0}", MyDel1(15,13));
Console.WriteLine("****************************************");
var trivInt = new Simple3<int>();
var trivString = new Simple3<string>();
Console.WriteLine("{0}", trivInt.ReturnIt(5));
Console.WriteLine("{0}", trivString.ReturnIt("Hi, there"));
Console.WriteLine("*****************************************");
Simple4 trivial = new Simple4();
Console.WriteLine("{0}", trivial.ReturnIt(5));
Console.WriteLine("{0}", trivial.ReturnIt("Hi, there."));
Console.WriteLine("******************************************");
Animal a1 = new Animal();
Animal a2 = new Dog();
Console.WriteLine("Number of dog legs: {0}", a2.NumberOfLegs);
Console.WriteLine("*******************************************");
Factory<Dog> dogMaker = MakeDog;
Factory<Animal> animalMaker = dogMaker;
Console.WriteLine(animalMaker().NumberOfLegs.ToString());
Console.WriteLine("********************************************");
Action1<Animal> act1 = ActionOnAnimal;
Action1<Dog> dog1 = act1;
dog1(new Dog());
Console.WriteLine("********************************************");
SimpleReturn<Dog> dogReturner = new SimpleReturn<Dog>();
dogReturner.items[0] = new Dog() {NumberOfLegs = 8888};
IMyIfc1<Animal> animalReturner = dogReturner;
DoSomething(animalReturner);
}
}
}