Move portable thread pool to shared partition (dotnet/corert#7828)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / ParamsArray.cs
blob9ab86fa5167547427bea0ae6de319be0ce25e86a
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.
5 namespace System
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)
26 _arg0 = arg0;
27 _arg1 = null;
28 _arg2 = null;
30 // Always assign this.args to make use of its "Length" property
31 _args = s_oneArgArray;
34 public ParamsArray(object? arg0, object? arg1)
36 _arg0 = arg0;
37 _arg1 = arg1;
38 _arg2 = null;
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)
46 _arg0 = arg0;
47 _arg1 = arg1;
48 _arg2 = 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;
60 _args = args;
63 public int Length => _args.Length;
65 public object? this[int index] => index == 0 ? _arg0 : GetAtSlow(index);
67 private object? GetAtSlow(int index)
69 if (index == 1)
70 return _arg1;
71 if (index == 2)
72 return _arg2;
73 return _args[index];