forked from cuicheng11165/clr-via-csharp-4th-edition-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ch09-1-Parameters.cs
191 lines (147 loc) · 5.07 KB
/
Ch09-1-Parameters.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
181
182
183
184
185
186
187
188
189
190
191
#define V1
//#define V2
//#define V3
using System;
using System.Collections.Generic;
public static class Parameters {
public static void Main() {
OptionalAndNamedParameters.Go();
MethodsThatTakeVariableArguments.Go();
}
}
internal static class OptionalAndNamedParameters {
private static Int32 s_n = 0;
public static void Go() {
ImplicitlyTypedLocalVariables();
// 1. Same as: M(9, "A", default(DateTime), new Guid());
M();
// 2. Same as: M(8, "X", default(DateTime), new Guid());
M(8, "X");
// 3. Same as: M(5, "A", DateTime.Now, Guid.NewGuid());
M(5, guid: Guid.NewGuid(), dt: DateTime.Now);
// 4. Same as: M(0, "1", default(DateTime), new Guid());
M(s_n++, s_n++.ToString());
// 5. Same as: String t1 = "2"; Int32 t2 = 3;
// M(t2, t1, default(DateTime), new Guid());
M(s: (s_n++).ToString(), x: s_n++);
}
private static void M(Int32 x = 9, String s = "A",
DateTime dt = default(DateTime), Guid guid = new Guid()) {
Console.WriteLine("x={0}, s={1}, dt={2}, guid={3}", x, s, dt, guid);
}
private static void ImplicitlyTypedLocalVariables() {
var name = "Jeff";
ShowVariableType(name); // Displays: System.String
// var n = null; // Error
var x = (Exception)null; // OK, but not much value
ShowVariableType(x); // Displays: System.Exception
var numbers = new Int32[] { 1, 2, 3, 4 };
ShowVariableType(numbers); // Displays: System.Int32[]
// Less typing for complex types
var collection = new Dictionary<String, Single>() { { ".NET", 4.0f } };
// Displays: System.Collections.Generic.Dictionary`2[System.String,System.Single]
ShowVariableType(collection);
foreach (var item in collection) {
// Displays: System.Collections.Generic.KeyValuePair`2[System.String,System.Single]
ShowVariableType(item);
}
}
private static void ShowVariableType<T>(T t) { Console.WriteLine(typeof(T)); }
}
internal static class OutAndRefParameters {
#if V1
public static void Go() {
Int32 x; // x is uninitialized
SetVal(out x); // x doesn’t have to be initialized.
Console.WriteLine(x); // Displays "10"
}
private static void SetVal(out Int32 v) {
v = 10; // This method must initialize v.
}
#endif
#if V2
public static void Main() {
Int32 x = 5; // x is initialized
AddVal(ref x); // x must be initialized.
Console.WriteLine(x); // Displays "15"
}
private static void AddVal(ref Int32 v) {
v += 10; // This method can use the initialized value in v.
}
#endif
#if V3
public static void Main() {
Int32 x; // x is not initialized.
// The following line fails to compile, producing
// error CS0165: Use of unassigned local variable 'x'.
AddVal(ref x);
Console.WriteLine(x);
}
private static void AddVal(ref Int32 v) {
v += 10; // This method can use the initialized value in v.
}
#endif
public static void Swap(ref Object a, ref Object b) {
Object t = b;
b = a;
a = t;
}
#if true
public static void SomeMethod() {
String s1 = "Jeffrey";
String s2 = "Richter";
Swap(ref s1, ref s2);
Console.WriteLine(s1); // Displays "Richter"
Console.WriteLine(s2); // Displays "Jeffrey"
}
#endif
public static void SomeMethod2() {
String s1 = "Jeffrey";
String s2 = "Richter";
// Variables that are passed by reference
// must match what the method expects.
Object o1 = s1, o2 = s2;
Swap(ref o1, ref o2);
// Now cast the objects back to strings.
s1 = (String)o1;
s2 = (String)o2;
Console.WriteLine(s1); // Displays "Richter"
Console.WriteLine(s2); // Displays "Jeffrey"
}
public static void Swap<T>(ref T a, ref T b) {
T t = b;
b = a;
a = t;
}
}
internal static class MethodsThatTakeVariableArguments {
public static void Go() {
// Displays "15"
Console.WriteLine(Add(new Int32[] { 1, 2, 3, 4, 5 }));
// Displays "15"
Console.WriteLine(Add(1, 2, 3, 4, 5));
// Displays "0"
Console.WriteLine(Add());
DisplayTypes(new Object(), new Random(), "Jeff", 5);
}
private static Int32 Add(params Int32[] values) {
// NOTE: it is possible to pass the 'values'
// array to other methods if you want to.
Int32 sum = 0;
for (Int32 x = 0; x < values.Length; x++)
sum += values[x];
return sum;
}
private static void DisplayTypes(params Object[] objects) {
foreach (Object o in objects)
Console.WriteLine(o.GetType());
}
}
///////////////////////////////////////////////////////////////////////////////
public sealed class Point {
static void Add(Point p) { /* ... */ }
static void Add(ref Point p) { /* ... */ }
// 'Add' cannot define overloaded methods that differ only on ref and out
// static void Add(out Point p) { /* ... */ }
}
////////////////////////////// End of File ////////////////////////////////////