-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathScoped.cs
More file actions
59 lines (51 loc) · 1.16 KB
/
Scoped.cs
File metadata and controls
59 lines (51 loc) · 1.16 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
public struct S1 { }
public ref struct S2 { }
// The `scoped` modifier can be applied to parameters
// or local variables. The type of the parameter or
// local variable must be either a `ref` value or `ref struct` value.
public class ScopedModifierTest
{
public ref int M1(scoped ref int x1, ref int y1)
{
// Not allowed.
// return ref x1;
return ref y1;
}
public ref int M2(scoped out int x2, ref int y2)
{
x2 = 0;
// Not allowed.
// return ref x;
return ref y2;
}
public int M3(scoped ref int x3)
{
// Allowed is it is not returned by reference.
return x3;
}
public S1 M4(scoped ref S1 x4)
{
// Allowed as it is not returned by reference.
return x4;
}
public S2 M5(scoped S2 x5)
{
// Not allowed.
// return x5;
return new S2();
}
public S2 M6(scoped ref S2 x6)
{
// Not allowed.
// return x6;
return new S2();
}
public S2 Locals()
{
scoped S2 x7 = new S2();
// Not allowed.
// return x7;
S2 y7 = new S2();
return y7;
}
}