A Method to Perform Array Value Equality Comparisons


Recall our earlier discussion about reference and value equality. Equipped with an understanding for how array object references are passed as arguments to a method, we can now design a method that determines whether two arrays are value equal to each other. The ValueEquals method (lines 21 41) of Listing 10.9 contains this functionality. The Main() method demonstrates how ValueEquals is used. It declares two array variables each referencing different objects, but with identical sets of array elements (lines 7 and 8). The following sample output verifies the difference between value equality as performed by the ValueEquals method, and reference equality performed by the equals comparison operator ==.

Listing 10.9 ValueEquality.cs
01: using System; 02: 03: class ValueEquality 04: { 05:     public static void Main() 06:     { 07:         int [] myArray = { 10, 20, 30, 40, 50, 60} ; 08:         int [] yourArray = { 10, 20, 30, 40, 50, 60} ; 09: 10:         if (ValueEquals (myArray, yourArray)) 11:             Console.WriteLine("myArray is value equal to yourArray"); 12:         else 13:             Console.WriteLine("myArray is not value equal to yourArray"); 14: 15:         if (myArray == yourArray) 16:             Console.WriteLine("myArray is reference equal to yourArray"); 17:         else 18:             Console.WriteLine("myArray is not reference equal to yourArray"); 19:     } 20: 21:     private static bool ValueEquals (int [] array1, int [] array2) 22:     { 23:         bool areValueEqual = true; 24:  25:         if (!(array1.Length == array2.Length)) 26:         { 27:             areValueEqual = false; 28:         } 29:         else 30:         { 31:             for (int i = 0; i < array1.Length; i++) 32:             { 33:                 if (!(array1[i] == array2[i])) 34:                 { 35:                     areValueEqual = false; 36:                     break; 37:                 } 38:             } 39:         } 40:         return areValueEqual; 41:     } 42: } myArray is value equal to yourArray myArray is not reference equal to yourArray 

Let's first look at the ValueEquals method spanning lines 21 41. Its header specifies two formal parameters (array1 and array2), both array variables of base type int. When ValueEquals is called, we must provide two array references as arguments (see line 10). The inner logic of ValueEquals follows the definition of value equal. First, the lengths of the two arrays must be identical (checked in lines 25 28), and second, each array element of one array (array1) must be identical to the array element with the corresponding index in the other array (array2) (see line 33). If any of these conditions are false, the two arrays are not value equal and the bool variable areValueEqual is set to false in line 27 or line 35. Notice that because the initial value of areValueEqual is set to true (in line 27), it will remain true until the method is terminated (in line 40) unless any of the previous conditions trigger it to be set to false. As specified in the method header, the method returns a value of type bool. This value will, when the method returns, be identical to the final value of areEqualValue, and only true if array1 and array2 are value equal.

The Main() method demonstrates the use of ValueEquals by declaring two array variables referencing two different array variables that are value equal. As expected (see sample output), the Boolean expression ValueEquals (myArray, yourArray) in line 10 returns the value true, whereas myArray == yourArray is false.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net