Skip to content

Commit dd71aa1

Browse files
committed
Adding notes on passing arrays around.
1 parent bc8939c commit dd71aa1

4 files changed

Lines changed: 87 additions & 4 deletions

File tree

source/code/projects/2DarrayExercise/2DarrayExercise/Program.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ static void Main()
1818
Console.WriteLine("We now rotate the array.");
1919
Rotate(ref matrix1);
2020
Display(matrix1);
21-
21+
2222
// Example array2
2323
int[,] matrix2 =
2424
{
2525
{ 1, 2, 3, 4 },
2626
{ 5, 6, 7, 8 },
27-
{ 9, 10, 11, 12 }
27+
{ 9, 10, 11, 12 },
2828
};
2929
// End of example array2
3030
Console.WriteLine("Example 2:");
@@ -64,9 +64,12 @@ static void Rotate(ref int[,] matP)
6464
{
6565
tmp[row, col] = matP[
6666
// This would be incorrect:
67-
// tmp.GetLength(0) - col, row
67+
// tmp.GetLength(0) - col, row
6868
// can you figure out why?
69-
tmp.GetLength(1) - col - 1, row
69+
tmp.GetLength(1)
70+
- col
71+
- 1,
72+
row
7073
];
7174
}
7275
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
class Demo
4+
{
5+
private int[] values;
6+
public int[] Values
7+
{
8+
get { return values; }
9+
}
10+
11+
private int value;
12+
public int Value
13+
{
14+
get { return value; }
15+
}
16+
17+
public Demo(int[] valuesP, int valueP)
18+
{
19+
values = valuesP;
20+
value = valueP;
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
int[] values = { 1, 2, 3 };
8+
int value = 0;
9+
Demo test = new Demo(values, value);
10+
11+
Console.WriteLine(test.Value);
12+
for (int i = 0; i < test.Values.Length; i++)
13+
{
14+
Console.WriteLine(test.Values[i]);
15+
}
16+
17+
// test.Value = 10;
18+
// error CS0200: Property or indexer 'Demo.Value' cannot be assigned to -- it is read only
19+
Console.WriteLine(test.Value);
20+
// still displays "0"
21+
test.Values[0] = 10;
22+
// This works!
23+
for (int i = 0; i < test.Values.Length; i++)
24+
{
25+
Console.WriteLine(test.Values[i]);
26+
}
27+
// Now displays
28+
// 10
29+
// 2
30+
// 3
31+
}
32+
}

source/lectures/misc/references.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,29 @@ static void Dummy(out int a)
276276
```
277277

278278
would not compile, as C# would give back a message "The out parameter 'a' must be assigned to before controls leaves the current method": an argument passed using the keyword `out` **must** be initialized in the body of the method.
279+
280+
## Security Concerns
281+
282+
Note that reference types requires greater care.
283+
284+
Consider the following `Demo` class implementation:
285+
286+
287+
```{download="./code/projects/ReferenceAndProperties.zip"}
288+
!include code/projects/ReferenceAndProperties/ReferenceAndProperties/Demo.cs
289+
```
290+
291+
One may think that `values` is "read-only" and can only be given a value when a new object is created, since the `Values` property does not implement a `set` method.
292+
However, **this is wrong**, as we show now:
293+
294+
```{download="./code/projects/ReferenceAndProperties.zip"}
295+
!include code/projects/ReferenceAndProperties/ReferenceAndProperties/Program.cs
296+
```
297+
298+
The following would compile and execute properly, **allowing to change the values in the `Values` array since *its reference* was passed to the `Main` method**.
299+
300+
To avoid this issue, one would have to return a *copy* of the array, for example by copying the array using a `for` loop and returning the copy, or by using the `Clone` method, as follows:
301+
302+
```
303+
get { return (int[])values.Clone(); }
304+
```

0 commit comments

Comments
 (0)