Small refactoring of method
authorThomas
Thu, 17 Dec 2020 07:55:08 +0100
changeset 3386 0e6b721cfb3f
parent 3385 f3f52637231b
child 3387 f3f7313f5cb3
Small refactoring of method
Comparisons.cs
--- a/Comparisons.cs	Wed Dec 16 14:25:23 2020 +0100
+++ b/Comparisons.cs	Thu Dec 17 07:55:08 2020 +0100
@@ -1,4 +1,5 @@
-´╗┐using System.Collections;
+´╗┐using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
@@ -37,11 +38,7 @@
         public static bool Equals(IEnumerable value1,
                                   IEnumerable value2)
         {
-            bool exists1;
-            bool exists2;
-            IEnumerator enumerator1;
-            IEnumerator enumerator2;
-
+            // Handle null cases
             if ((value1 == null) ||
                 (value2 == null))
             {
@@ -51,41 +48,47 @@
                     // One is null while the other is not
                     return false;
                 }
+
+                // Both are null
+                return true;
             }
             else
             {
-                enumerator1 = value1.GetEnumerator();
-                enumerator2 = value2.GetEnumerator();
-
-                try
+                if ((value1.GetEnumerator() is IEnumerator enumerator1) &&
+                    (value2.GetEnumerator() is IEnumerator enumerator2))
                 {
-                    exists1 = enumerator1.MoveNext();
-                    exists2 = enumerator2.MoveNext();
-
-                    while (exists1 && exists2)
+                    while (true)
                     {
-                        if (Comparisons.Equals(enumerator1.Current, enumerator2.Current) == false)
+                        try
                         {
+                            /* First check if one enumerator reached its end while the other hasn't,
+                             * in which case the values aren't of the same length and therefore not equal.
+                             */
+                            if (enumerator1.MoveNext() == false)
+                            {
+                                return (enumerator2.MoveNext() == false);
+                            }
+                            else if (enumerator2.MoveNext() == false)
+                            {
+                                return false;
+                            }
+                            else if (Comparisons.Equals(enumerator1.Current, enumerator2.Current) == false)
+                            {
+                                return false;
+                            }
+                        }
+                        catch (Exception ex)
+                        {
+                            Log.Error("Equals: Error occured. " + ex.ToString());
                             return false;
                         }
-
-                        exists1 = enumerator1.MoveNext();
-                        exists2 = enumerator2.MoveNext();
-                    }
-
-                    // If one of the enumerators still has a value, the lengths don't match
-                    if (exists1 || exists2)
-                    {
-                        return false;
                     }
                 }
-                catch
+                else
                 {
                     return false;
                 }
             }
-
-            return true;
         }
 
         /// <summary>