Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit efee815

Browse files
benaadamsjkotas
authored andcommitted
Use List<T>.ToArray() in places (#22101)
* Use ReadOnlySpan.ToArray in places * Use List.ToArray * Don't take .Length in to local and use as loop condition
1 parent 170854b commit efee815

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,9 +1391,8 @@ internal static string[] GetAllDateTimes(DateTime dateTime, DateTimeFormatInfo d
13911391
results.Add(strings[j]);
13921392
}
13931393
}
1394-
string[] value = new string[results.Count];
1395-
results.CopyTo(0, value, 0, results.Count);
1396-
return (value);
1394+
1395+
return results.ToArray();
13971396
}
13981397

13991398
// This is a placeholder for an MDA to detect when the user is using a

src/System.Private.CoreLib/src/System/RtType.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,8 +4097,7 @@ public override object InvokeMember(
40974097
if (results != null)
40984098
{
40994099
Debug.Assert(results.Count > 1);
4100-
finalists = new MethodInfo[results.Count];
4101-
results.CopyTo(finalists);
4100+
finalists = results.ToArray();
41024101
}
41034102
}
41044103

@@ -4149,8 +4148,7 @@ public override object InvokeMember(
41494148
if (results != null)
41504149
{
41514150
Debug.Assert(results.Count > 1);
4152-
finalists = new MethodInfo[results.Count];
4153-
results.CopyTo(finalists);
4151+
finalists = results.ToArray();
41544152
}
41554153
}
41564154

@@ -4342,8 +4340,6 @@ internal object CreateInstanceImpl(
43424340
if (args == null)
43434341
args = Array.Empty<object>();
43444342

4345-
int argCnt = args.Length;
4346-
43474343
// Without a binder we need to do use the default binder...
43484344
if (binder == null)
43494345
binder = DefaultBinder;
@@ -4352,7 +4348,7 @@ internal object CreateInstanceImpl(
43524348
// so a call to GetMemberCons would fail
43534349
bool publicOnly = (bindingAttr & BindingFlags.NonPublic) == 0;
43544350
bool wrapExceptions = (bindingAttr & BindingFlags.DoNotWrapExceptions) == 0;
4355-
if (argCnt == 0 && (bindingAttr & BindingFlags.Public) != 0 && (bindingAttr & BindingFlags.Instance) != 0
4351+
if (args.Length == 0 && (bindingAttr & BindingFlags.Public) != 0 && (bindingAttr & BindingFlags.Instance) != 0
43564352
&& (IsGenericCOMObjectImpl() || IsValueType))
43574353
{
43584354
server = CreateInstanceDefaultCtor(publicOnly, false, true, wrapExceptions);
@@ -4363,8 +4359,8 @@ internal object CreateInstanceImpl(
43634359
List<MethodBase> matches = new List<MethodBase>(candidates.Length);
43644360

43654361
// We cannot use Type.GetTypeArray here because some of the args might be null
4366-
Type[] argsType = new Type[argCnt];
4367-
for (int i = 0; i < argCnt; i++)
4362+
Type[] argsType = new Type[args.Length];
4363+
for (int i = 0; i < args.Length; i++)
43684364
{
43694365
if (args[i] != null)
43704366
{
@@ -4378,16 +4374,13 @@ internal object CreateInstanceImpl(
43784374
matches.Add(candidates[i]);
43794375
}
43804376

4381-
MethodBase[] cons = new MethodBase[matches.Count];
4382-
matches.CopyTo(cons);
4383-
if (cons != null && cons.Length == 0)
4384-
cons = null;
4385-
4386-
if (cons == null)
4377+
if (matches.Count == 0)
43874378
{
43884379
throw new MissingMethodException(SR.Format(SR.MissingConstructor_Name, FullName));
43894380
}
43904381

4382+
MethodBase[] cons = matches.ToArray();
4383+
43914384
MethodBase invokeMethod;
43924385
object state = null;
43934386

0 commit comments

Comments
 (0)