1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
7 internal readonly struct ParamsArray
9 // Sentinel fixed-length arrays eliminate the need for a "count" field keeping this
10 // struct down to just 4 fields. These are only used for their "Length" property,
11 // that is, their elements are never set or referenced.
12 private static readonly object?[] s_oneArgArray
= new object?[1];
13 private static readonly object?[] s_twoArgArray
= new object?[2];
14 private static readonly object?[] s_threeArgArray
= new object?[3];
16 private readonly object? _arg0
;
17 private readonly object? _arg1
;
18 private readonly object? _arg2
;
20 // After construction, the first three elements of this array will never be accessed
21 // because the indexer will retrieve those values from arg0, arg1, and arg2.
22 private readonly object?[] _args
;
24 public ParamsArray(object? arg0
)
30 // Always assign this.args to make use of its "Length" property
31 _args
= s_oneArgArray
;
34 public ParamsArray(object? arg0
, object? arg1
)
40 // Always assign this.args to make use of its "Length" property
41 _args
= s_twoArgArray
;
44 public ParamsArray(object? arg0
, object? arg1
, object? arg2
)
50 // Always assign this.args to make use of its "Length" property
51 _args
= s_threeArgArray
;
54 public ParamsArray(object?[] args
)
56 int len
= args
.Length
;
57 _arg0
= len
> 0 ? args
[0] : null;
58 _arg1
= len
> 1 ? args
[1] : null;
59 _arg2
= len
> 2 ? args
[2] : null;
63 public int Length
=> _args
.Length
;
65 public object? this[int index
] => index
== 0 ? _arg0
: GetAtSlow(index
);
67 private object? GetAtSlow(int index
)