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

[mscorlib] Prefer using Array.Length as upper for loop limit #8923

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/mscorlib/src/System/MulticastDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,9 @@ public override sealed Delegate[] GetInvocationList()
{
// Create an array of delegate copies and each
// element into the array
int invocationCount = (int)_invocationCount;
del = new Delegate[invocationCount];
del = new Delegate[(int)_invocationCount];

for (int i = 0; i < invocationCount; i++)
for (int i = 0; i < del.Length; i++)
del[i] = (Delegate)invocationList[i];
}
return del;
Expand Down
14 changes: 7 additions & 7 deletions src/mscorlib/src/System/Reflection/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,19 +573,18 @@ public virtual Type[] GetTypes()
{
Module[] m = GetModules(false);

int iNumModules = m.Length;
int iFinalLength = 0;
Type[][] ModuleTypes = new Type[iNumModules][];
Type[][] ModuleTypes = new Type[m.Length][];

for (int i = 0; i < iNumModules; i++)
for (int i = 0; i < ModuleTypes.Length; i++)
{
ModuleTypes[i] = m[i].GetTypes();
iFinalLength += ModuleTypes[i].Length;
}

int iCurrent = 0;
Type[] ret = new Type[iFinalLength];
for (int i = 0; i < iNumModules; i++)
for (int i = 0; i < ModuleTypes.Length; i++)
{
int iLength = ModuleTypes[i].Length;
Array.Copy(ModuleTypes[i], 0, ret, iCurrent, iLength);
Expand Down Expand Up @@ -1515,13 +1514,14 @@ public override FileStream GetFile(String name)
public override FileStream[] GetFiles(bool getResourceModules)
{
Module[] m = GetModules(getResourceModules);
int iLength = m.Length;
FileStream[] fs = new FileStream[iLength];
FileStream[] fs = new FileStream[m.Length];

for(int i = 0; i < iLength; i++)
for (int i = 0; i < fs.Length; i++)
{
fs[i] = new FileStream(((RuntimeModule)m[i]).GetFullyQualifiedName(),
FileMode.Open,
FileAccess.Read, FileShare.Read, FileStream.DefaultBufferSize, false);
}

return fs;
}
Expand Down
20 changes: 8 additions & 12 deletions src/mscorlib/src/System/Reflection/Emit/DynamicILGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,22 +520,18 @@ internal override SignatureHelper GetMemberRefSignature(
Type[] parameterTypes,
Type[] optionalParameterTypes)
{
int cParams;
int i;
SignatureHelper sig;
if (parameterTypes == null)
cParams = 0;
else
cParams = parameterTypes.Length;
sig = SignatureHelper.GetMethodSigHelper(call, returnType);
for (i = 0; i < cParams; i++)
sig.AddArgument(parameterTypes[i]);
SignatureHelper sig = SignatureHelper.GetMethodSigHelper(call, returnType);
if (parameterTypes != null)
{
foreach (Type t in parameterTypes)
sig.AddArgument(t);
}
if (optionalParameterTypes != null && optionalParameterTypes.Length != 0)
{
// add the sentinel
sig.AddSentinel();
for (i = 0; i < optionalParameterTypes.Length; i++)
sig.AddArgument(optionalParameterTypes[i]);
foreach (Type t in optionalParameterTypes)
sig.AddArgument(t);
}
return sig;
}
Expand Down
11 changes: 4 additions & 7 deletions src/mscorlib/src/System/Reflection/Emit/ILGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,17 @@ private static void SortExceptions(__ExceptionInfo []exceptions)
// Just a cheap insertion sort. We don't expect many exceptions (<10), where InsertionSort beats QuickSort.
// If we have more exceptions than this in real life, we should consider moving to a QuickSort.

int least;
__ExceptionInfo temp;
int length = exceptions.Length;
for (int i =0; i < length; i++)
for (int i = 0; i < exceptions.Length; i++)
{
least = i;
for (int j =i + 1; j < length; j++)
int least = i;
for (int j = i + 1; j < exceptions.Length; j++)
{
if (exceptions[least].IsInner(exceptions[j]))
{
least = j;
}
}
temp = exceptions[i];
__ExceptionInfo temp = exceptions[i];
exceptions[i] = exceptions[least];
exceptions[least] = temp;
}
Expand Down
17 changes: 9 additions & 8 deletions src/mscorlib/src/System/Reflection/Emit/ModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,11 @@ internal MethodToken InternalGetConstructorToken(ConstructorInfo con, bool using
if (parameters == null)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidConstructorInfo"));

int count = parameters.Length;
Type[] parameterTypes = new Type[count];
Type[][] requiredCustomModifiers = new Type[count][];
Type[][] optionalCustomModifiers = new Type[count][];
Type[] parameterTypes = new Type[parameters.Length];
Type[][] requiredCustomModifiers = new Type[parameters.Length][];
Type[][] optionalCustomModifiers = new Type[parameters.Length][];

for (int i = 0; i < count; i++)
for (int i = 0; i < parameters.Length; i++)
{
if (parameters[i] == null)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidConstructorInfo"));
Expand Down Expand Up @@ -603,12 +602,14 @@ private int GetMemberRefToken(MethodBase method, IEnumerable<Type> optionalParam
internal SignatureHelper GetMemberRefSignature(CallingConventions call, Type returnType,
Type[] parameterTypes, IEnumerable<Type> optionalParameterTypes, int cGenericParameters)
{
int cParams = (parameterTypes == null) ? 0 : parameterTypes.Length;
SignatureHelper sig = SignatureHelper.GetMethodSigHelper(this, call, returnType, cGenericParameters);

for (int i = 0; i < cParams; i++)
if (parameterTypes != null)
{
sig.AddArgument(parameterTypes[i]);
foreach (Type t in parameterTypes)
{
sig.AddArgument(t);
}
}

if (optionalParameterTypes != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/mscorlib/src/System/Reflection/MdImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public unsafe String GetUserString(int mdToken)
return null;

char[] c = new char[length];
for (int i = 0; i < length; i ++)
for (int i = 0; i < c.Length; i ++)
{
#if ALIGN_ACCESS
c[i] = (char)Marshal.ReadInt16( (IntPtr) (((char*)name) + i) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ internal Container Resize(int newSize)
// Reallocate both buckets and entries and rebuild the bucket and entries from scratch.
// This serves both to scrub entries with expired keys and to put the new entries in the proper bucket.
int[] newBuckets = new int[newSize];
for (int bucketIndex = 0; bucketIndex < newSize; bucketIndex++)
for (int bucketIndex = 0; bucketIndex < newBuckets.Length; bucketIndex++)
{
newBuckets[bucketIndex] = -1;
}
Expand Down