Skip to content

Commit

Permalink
Use Array.Empty<T>() (dotnet/extensions#1210)
Browse files Browse the repository at this point in the history
Use Array.Empty<string>() instead of allocating a new array if either of the strings for comparison are null.


Commit migrated from dotnet/extensions@afb772f
  • Loading branch information
martincostello authored and Tratcher committed Mar 4, 2019
1 parent fb9befb commit e51081b
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class ConfigurationKeyComparer : IComparer<string>
/// <returns></returns>
public int Compare(string x, string y)
{
var xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
var yParts = y?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
var xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
var yParts = y?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();

// Compare each part until we get two parts that are not equal
for (int i = 0; i < Math.Min(xParts.Length, yParts.Length); i++)
Expand All @@ -41,7 +41,7 @@ public int Compare(string x, string y)
var xIsInt = x != null && int.TryParse(x, out value1);
var yIsInt = y != null && int.TryParse(y, out value2);

int result = 0;
int result;

if (!xIsInt && !yIsInt)
{
Expand Down

0 comments on commit e51081b

Please sign in to comment.