-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_VariablesAndParameters.cs
199 lines (155 loc) · 6.61 KB
/
04_VariablesAndParameters.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
192
193
194
195
196
197
198
199
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using basic.Common;
using Xunit;
namespace basic
{
public class VariablesAndParameters
{
[Fact]
public void should_initialize_to_default_value()
{
var defaultValueDemo = new DefaultValueDemoClass();
// change the variable values of the following 2 lines to correct values
RefTypeClass expectedReferenceTypeValue = null;
const int expectedValueTypeValue = 0;
Assert.Equal(expectedReferenceTypeValue, defaultValueDemo.referenceTypeValue);
Assert.Equal(expectedValueTypeValue, defaultValueDemo.valueTypeValue);
}
#pragma warning disable xUnit2000
[Fact]
public void should_get_default_value_using_default_operator()
{
// change the variable values of the following 4 lines to correct values.
const int expectedDefaultIntResult = 0;
const bool expectedDefaultBoolResult = false;
const char expectedDefaultCharResult = '\0';
object expectedDefaultObjectResult = null;
Assert.Equal(expectedDefaultIntResult, default(int));
Assert.Equal(expectedDefaultBoolResult, default(bool));
Assert.Equal(expectedDefaultCharResult, default(char));
Assert.Equal(expectedDefaultObjectResult, default(object));
}
#pragma warning restore xUnit2000
[Fact]
public void should_get_copy_of_the_argument_when_passing_by_value_for_value_type()
{
int passingInt = 1;
// change the variable value to correct one.
const int expectedResult = 1;
FunctionPassingIntAsArgument(passingInt);
Assert.Equal(expectedResult, passingInt);
}
[Fact]
[Description("https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/ref")]
public void should_get_copy_of_the_argument_when_passing_by_value_for_ref_type()
{
var refTypeObject = new RefTypeClass(1);
RefTypeClass modifiedRefTypeObject = FunctionPassingRefTypeClassAsArgument(refTypeObject);
// change the variable value to correct one.
RefTypeClass expectedResult = refTypeObject;
Assert.Same(expectedResult, refTypeObject);
}
[Fact]
public void should_ref_to_same_location_when_passing_by_ref_for_value_type()
{
int passingInt = 1;
// change the variable value to correct one.
const int expectedResult = 2;
FunctionPassingRefIntAsArgument(ref passingInt);
Assert.Equal(expectedResult, passingInt);
}
[Fact]
[SuppressMessage("ReSharper", "UnusedVariable")]
public void should_ref_to_same_location_when_passing_by_ref_for_ref_type()
{
var refTypeObject = new RefTypeClass(1);
RefTypeClass refToOriginalObject = refTypeObject;
RefTypeClass modifiedRefTypeObject = FunctionPassingRefRefTypeClassAsArgument(
ref refTypeObject);
// change the variable value to correct one
object expectedResult = modifiedRefTypeObject;
Assert.Same(expectedResult, refTypeObject);
}
[Fact]
[SuppressMessage("ReSharper", "InlineOutVariableDeclaration")]
public void should_ref_to_same_location_when_passing_by_out_for_value_type()
{
int passingInt;
FunctionPassingOutIntAsArgument(out passingInt);
// change the variable value to correct one
const int expectedResult = 2;
Assert.Equal(expectedResult, passingInt);
}
[Fact]
[SuppressMessage("ReSharper", "InlineOutVariableDeclaration")]
[SuppressMessage("ReSharper", "UnusedVariable")]
public void should_ref_to_same_location_when_passing_by_out_for_ref_type()
{
RefTypeClass refTypeObject;
RefTypeClass modifiedRefTypeObject =
FunctionPassingOutRefTypeClassAsArgument(out refTypeObject);
// change the variable value to correct one
object expectedResult = modifiedRefTypeObject;
Assert.Equal(expectedResult, refTypeObject);
}
[Fact]
public void should_pass_variable_length_parameter_as_array()
{
int sum = PassVariableLengthArguments(1, 2, 3, 4, 5);
// change the variable value to correct one
const int expectedResult = 15;
Assert.Equal(expectedResult, sum);
}
[Fact]
public void should_pass_as_optional_parameter()
{
int optionalParameterValue = PassAsOptionalArgument();
// change the variable value to correct one
const int expectedResult = 23;
Assert.Equal(expectedResult, optionalParameterValue);
}
private static int PassAsOptionalArgument(int value = 23)
{
return value;
}
private static int PassVariableLengthArguments(params int[] values)
{
return values.Sum(i => i);
}
private static RefTypeClass FunctionPassingOutRefTypeClassAsArgument(
out RefTypeClass refTypeObject)
{
refTypeObject = new RefTypeClass(1);
return refTypeObject;
}
private static void FunctionPassingOutIntAsArgument(out int passingInt)
{
passingInt = 2;
}
[SuppressMessage("ReSharper", "RedundantAssignment")]
private RefTypeClass FunctionPassingRefRefTypeClassAsArgument(
ref RefTypeClass refTypeObject)
{
refTypeObject = new RefTypeClass(2);
return refTypeObject;
}
private static void FunctionPassingRefIntAsArgument(ref int passingInt)
{
passingInt = passingInt * 2;
}
[SuppressMessage("ReSharper", "RedundantAssignment")]
private static RefTypeClass FunctionPassingRefTypeClassAsArgument(
RefTypeClass refTypeObject)
{
refTypeObject = new RefTypeClass(2);
return refTypeObject;
}
[SuppressMessage("ReSharper", "RedundantAssignment")]
private static void FunctionPassingIntAsArgument(int value)
{
value = value * 2;
}
}
}