-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericClass.cs
More file actions
147 lines (114 loc) · 3.01 KB
/
GenericClass.cs
File metadata and controls
147 lines (114 loc) · 3.01 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
using System.Collections.Generic;
// 数组: int
public class ArrayInt {
public int[] arr;
public ArrayInt(int size) {
arr = new int[size];
}
public void Add(int v) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == 0) {
arr[i] = v;
break;
}
}
}
public void Remove(int v) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == v) {
arr[i] = 0;
break;
}
}
}
public void ForeachLog() {
for (int i = 0; i < arr.Length; i++) {
System.Console.WriteLine(arr[i]);
}
}
}
public class ArrayGeneric<T> where T : class {
public T[] arr;
public ArrayGeneric(int size) {
arr = new T[size];
}
public void Add(T v) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == null) {
arr[i] = v;
break;
}
}
}
public void Remove(T v) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == v) {
arr[i] = null;
break;
}
}
}
public void ForeachLog() {
for (int i = 0; i < arr.Length; i++) {
System.Console.WriteLine(arr[i]);
}
}
}
public class Any<T> {
public T value;
public void Log() {
System.Console.WriteLine(value.ToString());
}
}
public class FakeDict<TKey, TValue> {
public TKey[] keys;
public TValue[] values;
public void Add(TKey key, TValue value) {
for (int i = 0; i < keys.Length; i++) {
if (keys[i] == null) {
keys[i] = key;
values[i] = value;
break;
}
}
}
public void Remove(TKey key) {
for (int i = 0; i < keys.Length; i++) {
if (keys[i].Equals(key)) {
keys[i] = default(TKey);
values[i] = default(TValue);
break;
}
}
}
}
public static class GenericClass {
public static void Entry() {
// ==== Ours ====
Any<int> anyInt = new Any<int>();
anyInt.value = 5;
anyInt.Log();
Any<string> anyStr = new Any<string>();
anyStr.value = "s";
anyStr.Log();
ArrayInt arrayInt = new ArrayInt(10);
arrayInt.Add(5);
arrayInt.Add(6);
arrayInt.ForeachLog();
ArrayGeneric<string> arrayStr = new ArrayGeneric<string>(5);
arrayStr.Add("Yo 1");
arrayStr.Add("Yo 2");
arrayStr.ForeachLog();
// ==== Microsoft ====
List<int> list = new List<int>();
List<string> list2 = new List<string>();
list.Add(3);
list.Add(5);
list.ForEach(value => {
System.Console.WriteLine(value.ToString());
});
FakeDict<int, string> fakeDict = new FakeDict<int, string>();
fakeDict.Add(10, "yo10");
fakeDict.Add(3, "yo3");
}
}