-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConversionOperators.cs
80 lines (60 loc) · 1.47 KB
/
ConversionOperators.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
class Basket {
public Basket() {
items = new string[capacity];
load_counter = 0;
}
public Basket(string text) {
capacity = 1;
items = new string[1];
load_counter = 1;
items[0] = text;
}
public static explicit operator Basket(string text) => new Basket(text);
public static implicit operator string(Basket basket) {
string text;
if (basket.load_counter > 0)
text = basket.items[0];
else text = "";
return text;
}
public void put(string item) {
if (load_counter < capacity) {
items[load_counter] = item;
load_counter++;
}
}
public string pull() {
string item;
if (load_counter > 0) {
item = items[load_counter - 1];
items[load_counter - 1] = "";
load_counter--;
} else {
item = "";
}
return item;
}
public void show() {
for (int i = 0; i < load_counter; i++) {
System.Console.Write(items[i] + " ");
}
System.Console.WriteLine();
}
private int capacity = 3;
private string[] items;
private int load_counter;
}
class ConvertingConstructor {
public static void Main(string[] args) {
Basket another_basket = new Basket("beetroot");
another_basket.show();
Basket handbasket = new Basket();
handbasket.put("apple");
handbasket.put("pear");
handbasket.put("plum");
string fruit = handbasket;
System.Console.WriteLine(fruit);
string food = (string)handbasket;
System.Console.WriteLine(food);
}
}