From efee81505366236a22a06663fc14927a516d5655 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 20 Jan 2019 23:42:10 +0100 Subject: Use List.ToArray() in places (#22101) * Use ReadOnlySpan.ToArray in places * Use List.ToArray * Don't take .Length in to local and use as loop condition --- .../shared/System/Globalization/DateTimeFormat.cs | 5 ++--- src/System.Private.CoreLib/src/System/RtType.cs | 23 ++++++++-------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs index ea7387589c..61143fecfd 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs @@ -1391,9 +1391,8 @@ namespace System results.Add(strings[j]); } } - string[] value = new string[results.Count]; - results.CopyTo(0, value, 0, results.Count); - return (value); + + return results.ToArray(); } // This is a placeholder for an MDA to detect when the user is using a diff --git a/src/System.Private.CoreLib/src/System/RtType.cs b/src/System.Private.CoreLib/src/System/RtType.cs index 921c15e25b..e423b45718 100644 --- a/src/System.Private.CoreLib/src/System/RtType.cs +++ b/src/System.Private.CoreLib/src/System/RtType.cs @@ -4097,8 +4097,7 @@ namespace System if (results != null) { Debug.Assert(results.Count > 1); - finalists = new MethodInfo[results.Count]; - results.CopyTo(finalists); + finalists = results.ToArray(); } } @@ -4149,8 +4148,7 @@ namespace System if (results != null) { Debug.Assert(results.Count > 1); - finalists = new MethodInfo[results.Count]; - results.CopyTo(finalists); + finalists = results.ToArray(); } } @@ -4342,8 +4340,6 @@ namespace System if (args == null) args = Array.Empty(); - int argCnt = args.Length; - // Without a binder we need to do use the default binder... if (binder == null) binder = DefaultBinder; @@ -4352,7 +4348,7 @@ namespace System // so a call to GetMemberCons would fail bool publicOnly = (bindingAttr & BindingFlags.NonPublic) == 0; bool wrapExceptions = (bindingAttr & BindingFlags.DoNotWrapExceptions) == 0; - if (argCnt == 0 && (bindingAttr & BindingFlags.Public) != 0 && (bindingAttr & BindingFlags.Instance) != 0 + if (args.Length == 0 && (bindingAttr & BindingFlags.Public) != 0 && (bindingAttr & BindingFlags.Instance) != 0 && (IsGenericCOMObjectImpl() || IsValueType)) { server = CreateInstanceDefaultCtor(publicOnly, false, true, wrapExceptions); @@ -4363,8 +4359,8 @@ namespace System List matches = new List(candidates.Length); // We cannot use Type.GetTypeArray here because some of the args might be null - Type[] argsType = new Type[argCnt]; - for (int i = 0; i < argCnt; i++) + Type[] argsType = new Type[args.Length]; + for (int i = 0; i < args.Length; i++) { if (args[i] != null) { @@ -4378,16 +4374,13 @@ namespace System matches.Add(candidates[i]); } - MethodBase[] cons = new MethodBase[matches.Count]; - matches.CopyTo(cons); - if (cons != null && cons.Length == 0) - cons = null; - - if (cons == null) + if (matches.Count == 0) { throw new MissingMethodException(SR.Format(SR.MissingConstructor_Name, FullName)); } + MethodBase[] cons = matches.ToArray(); + MethodBase invokeMethod; object state = null; -- cgit v1.2.3