Files
mono/netcore/System.Private.CoreLib/shared/System/ParamsArray.cs
T
Stephen Toub b31b703b89 Turn on nullability for all of Corelib (dotnet/coreclr#24497)
* Enable nullable at the project level

* Remove `#nullable enable` from individual files

Removes `#nullable enable` from almost all .cs files in System.Private.CoreLib. I left it only in the ~30 files (out of ~1480 that had it) that are mirrored to corefx, that are built into projects by corefx that don't yet set NullableContextOptions at the project level, and that use nullable annotations; otherwise, they'd break the corefx build.

Signed-off-by: dotnet-bot <[email protected]>
2019-05-10 10:51:17 +02:00

83 lines
2.5 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace System
{
internal readonly struct ParamsArray
{
// Sentinel fixed-length arrays eliminate the need for a "count" field keeping this
// struct down to just 4 fields. These are only used for their "Length" property,
// that is, their elements are never set or referenced.
private static readonly object?[] s_oneArgArray = new object?[1];
private static readonly object?[] s_twoArgArray = new object?[2];
private static readonly object?[] s_threeArgArray = new object?[3];
private readonly object? _arg0;
private readonly object? _arg1;
private readonly object? _arg2;
// After construction, the first three elements of this array will never be accessed
// because the indexer will retrieve those values from arg0, arg1, and arg2.
private readonly object?[] _args;
public ParamsArray(object? arg0)
{
_arg0 = arg0;
_arg1 = null;
_arg2 = null;
// Always assign this.args to make use of its "Length" property
_args = s_oneArgArray;
}
public ParamsArray(object? arg0, object? arg1)
{
_arg0 = arg0;
_arg1 = arg1;
_arg2 = null;
// Always assign this.args to make use of its "Length" property
_args = s_twoArgArray;
}
public ParamsArray(object? arg0, object? arg1, object? arg2)
{
_arg0 = arg0;
_arg1 = arg1;
_arg2 = arg2;
// Always assign this.args to make use of its "Length" property
_args = s_threeArgArray;
}
public ParamsArray(object?[] args)
{
int len = args.Length;
_arg0 = len > 0 ? args[0] : null;
_arg1 = len > 1 ? args[1] : null;
_arg2 = len > 2 ? args[2] : null;
_args = args;
}
public int Length
{
get { return _args.Length; }
}
public object? this[int index]
{
get { return index == 0 ? _arg0 : GetAtSlow(index); }
}
private object? GetAtSlow(int index)
{
if (index == 1)
return _arg1;
if (index == 2)
return _arg2;
return _args[index];
}
}
}