Skip to content

Commit 5cec571

Browse files
authored
Merge pull request #9334 from h3xds1nz/remove-getlength-on-single-dim
Replace Array.GetLength on single-dimensional arrays with Length property
2 parents 816c24a + 814e7da commit 5cec571

File tree

6 files changed

+29
-26
lines changed

6 files changed

+29
-26
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Color.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static Color FromProfile(Uri profileUri)
5757
if (c1.context != null)
5858
{
5959
c1.nativeColorValue = new float[c1.context.NumChannels];
60-
for (int i = 0; i < c1.nativeColorValue.GetLength(0); i++)
60+
for (int i = 0; i < c1.nativeColorValue.Length; i++)
6161
{
6262
c1.nativeColorValue[i] = 0.0f;
6363
}
@@ -80,12 +80,12 @@ public static Color FromAValues(float a, float[] values, Uri profileUri)
8080
throw new ArgumentException(SR.Format(SR.Color_DimensionMismatch, null));
8181
}
8282

83-
if (values.GetLength(0) != c1.nativeColorValue.GetLength(0))
83+
if (values.Length != c1.nativeColorValue.Length)
8484
{
8585
throw new ArgumentException(SR.Format(SR.Color_DimensionMismatch, null));
8686
}
8787

88-
for (int numChannels = 0; numChannels < values.GetLength(0); numChannels++)
88+
for (int numChannels = 0; numChannels < values.Length; numChannels++)
8989
{
9090
c1.nativeColorValue[numChannels] = values[numChannels];
9191
}
@@ -305,10 +305,10 @@ internal string ConvertToString(string format, IFormatProvider provider)
305305
var sb = new StringBuilder();
306306
sb.AppendFormat(provider, "{0}{1} ", Parsers.s_ContextColor, uriString);
307307
sb.AppendFormat(provider,"{1:" + format + "}{0}",separator,scRgbColor.a);
308-
for (int i= 0; i< nativeColorValue.GetLength(0); ++i )
308+
for (int i = 0; i < nativeColorValue.Length; ++i )
309309
{
310310
sb.AppendFormat(provider,"{0:" + format + "}",nativeColorValue[i]);
311-
if (i< nativeColorValue.GetLength(0)-1 )
311+
if (i < nativeColorValue.Length - 1)
312312
{
313313
sb.AppendFormat(provider,"{0}",separator);
314314
}
@@ -350,7 +350,7 @@ private bool IsClose(Color color)
350350
}
351351
else
352352
{
353-
for (int i = 0; i < color.nativeColorValue.GetLength(0); i++)
353+
for (int i = 0; i < color.nativeColorValue.Length; i++)
354354
result = result && FloatUtil.AreClose(nativeColorValue[i], color.nativeColorValue[i]);
355355
}
356356

@@ -421,9 +421,9 @@ public float[] GetNativeColorValues()
421421

422422
#pragma warning suppress 6506 // c1.context is obviously not null - both color1.context AND color2.context are not null
423423
c1.nativeColorValue = new float[c1.context.NumChannels];
424-
for (int i = 0; i < c1.nativeColorValue.GetLength(0); i++)
424+
for (int i = 0; i < c1.nativeColorValue.Length; i++)
425425
{
426-
c1.nativeColorValue[i] = color1.nativeColorValue[i] + color2.nativeColorValue[i] ;
426+
c1.nativeColorValue[i] = color1.nativeColorValue[i] + color2.nativeColorValue[i];
427427
}
428428

429429
Color c2 = Color.FromRgb(0, 0, 0);
@@ -540,7 +540,7 @@ public static Color Add(Color color1, Color color2)
540540

541541
#pragma warning suppress 6506 // c1.context is obviously not null - both color1.context AND color2.context are not null
542542
c1.nativeColorValue = new float[c1.context.NumChannels];
543-
for (int i = 0; i < c1.nativeColorValue.GetLength(0); i++)
543+
for (int i = 0; i < c1.nativeColorValue.Length; i++)
544544
{
545545
c1.nativeColorValue[i] = color1.nativeColorValue[i] - color2.nativeColorValue[i];
546546
}
@@ -750,12 +750,12 @@ public override bool Equals(object o)
750750
return false;
751751
}
752752

753-
if (color1.nativeColorValue.GetLength(0) != color2.nativeColorValue.GetLength(0))
753+
if (color1.nativeColorValue.Length != color2.nativeColorValue.Length)
754754
{
755755
return false;
756756
}
757757

758-
for (int i = 0; i < color1.nativeColorValue.GetLength(0); i++)
758+
for (int i = 0; i < color1.nativeColorValue.Length; i++)
759759
{
760760
if (color1.nativeColorValue[i] != color2.nativeColorValue[i])
761761
{

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,8 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
10851085

10861086
if (sourceBuffer.Rank == 1)
10871087
{
1088-
if (sourceBuffer.GetLength(0) <= 0)
1088+
int firstDimLength = sourceBuffer.GetLength(0);
1089+
if (firstDimLength == 0)
10891090
{
10901091
if (backwardsCompat)
10911092
{
@@ -1095,7 +1096,7 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
10951096
}
10961097
else
10971098
{
1098-
throw new ArgumentException(SR.Image_InsufficientBuffer, "sourceBuffer");
1099+
throw new ArgumentException(SR.Image_InsufficientBuffer, nameof(sourceBuffer));
10991100
}
11001101
}
11011102
else
@@ -1104,14 +1105,16 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
11041105
{
11051106
object exemplar = sourceBuffer.GetValue(0);
11061107
elementSize = Marshal.SizeOf(exemplar);
1107-
sourceBufferSize = sourceBuffer.GetLength(0) * elementSize;
1108+
sourceBufferSize = firstDimLength * elementSize;
11081109
elementType = exemplar.GetType();
11091110
}
11101111
}
11111112
}
11121113
else if (sourceBuffer.Rank == 2)
11131114
{
1114-
if (sourceBuffer.GetLength(0) <= 0 || sourceBuffer.GetLength(1) <= 0)
1115+
int firstDimLength = sourceBuffer.GetLength(0);
1116+
int secondDimLength = sourceBuffer.GetLength(1);
1117+
if (firstDimLength == 0 || secondDimLength == 0)
11151118
{
11161119
if (backwardsCompat)
11171120
{
@@ -1121,16 +1124,16 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
11211124
}
11221125
else
11231126
{
1124-
throw new ArgumentException(SR.Image_InsufficientBuffer, "sourceBuffer");
1127+
throw new ArgumentException(SR.Image_InsufficientBuffer, nameof(sourceBuffer));
11251128
}
11261129
}
11271130
else
11281131
{
11291132
checked
11301133
{
1131-
object exemplar = sourceBuffer.GetValue(0,0);
1134+
object exemplar = sourceBuffer.GetValue(0, 0);
11321135
elementSize = Marshal.SizeOf(exemplar);
1133-
sourceBufferSize = sourceBuffer.GetLength(0) * sourceBuffer.GetLength(1) * elementSize;
1136+
sourceBufferSize = (firstDimLength * secondDimLength) * elementSize;
11341137
elementType = exemplar.GetType();
11351138
}
11361139
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Parsers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static private Color ParseContextColor(string trimmedColor, IFormatProvider form
105105
string tokens = trimmedColor.Substring(s_ContextColor.Length);
106106
tokens = tokens.Trim();
107107
string[] preSplit = tokens.Split(' ');
108-
if (preSplit.GetLength(0)< 2)
108+
if (preSplit.Length < 2)
109109
{
110110
throw new FormatException(SR.Parsers_IllegalToken);
111111
}
@@ -114,7 +114,7 @@ static private Color ParseContextColor(string trimmedColor, IFormatProvider form
114114

115115
TokenizerHelper th = new TokenizerHelper(tokens, formatProvider);
116116
string[] split = tokens.Split(new Char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
117-
int numTokens = split.GetLength(0);
117+
int numTokens = split.Length;
118118

119119
float alpha = Convert.ToSingle(th.NextTokenRequired(), formatProvider);
120120

src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ColorTypeConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ Type destinationType
185185

186186
sb.AppendFormat(provider, "ContextColor {0} ", uriString);
187187
sb.AppendFormat(provider, "{1:R}{0}", separator, color.ScA);
188-
for (int i = 0; i < color.GetNativeColorValues().GetLength(0); ++i)
188+
for (int i = 0; i < color.GetNativeColorValues().Length; ++i)
189189
{
190190
sb.AppendFormat(provider, "{0:R}", color.GetNativeColorValues()[i]);
191-
if (i < color.GetNativeColorValues().GetLength(0) - 1)
191+
if (i < color.GetNativeColorValues().Length - 1)
192192
{
193193
sb.AppendFormat(provider, "{0}", separator);
194194
}
@@ -329,12 +329,12 @@ ColorContext colorContext
329329

330330
XpsResourceStream resourceStream = manager.AcquireResourceStream(typeof(ColorContext), colorContextMimeType.ToString());
331331

332-
byte [] buffer = new byte[512];
332+
byte[] buffer = new byte[512];
333333

334334
Stream profileStream = colorContext.OpenProfileStream();
335335
int count;
336336

337-
while ( (count = profileStream.Read( buffer, 0, buffer.GetLength(0)) ) > 0 )
337+
while ( (count = profileStream.Read( buffer, 0, buffer.Length) ) > 0 )
338338
{
339339
resourceStream.Stream.Write(buffer,0,count);
340340
}

src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachSerializationCacheItems.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ out DesignerSerializationOptionsAttribute designerSerializationFlagsAttr
218218
// and that are not hidden
219219
//
220220
if (propertyInfo.CanRead &&
221-
propertyInfo.GetIndexParameters().GetLength(0) == 0)
221+
propertyInfo.GetIndexParameters().Length == 0)
222222
{
223223
MemberInfo memberInfo = (MemberInfo) propertyInfo;
224224

src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/XmlSignatureProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private static DateTime XmlFormattedTimeToDateTime(String s, String format)
382382
/// <returns>-1 if not found</returns>
383383
private static int GetIndex(String format)
384384
{
385-
for (int i = 0; i < _dateTimePatternMap.GetLength(0); i++)
385+
for (int i = 0; i < _dateTimePatternMap.Length; i++)
386386
{
387387
if (string.Equals(_dateTimePatternMap[i].Format, format, StringComparison.Ordinal))
388388
{

0 commit comments

Comments
 (0)