I am trying to compare two .NET arrays. Here is a clear implementation to compare the arrays of bytes:
bool AreEqual (byte [] a, byte [] b) {if (a.Length! = B.Length) Return; For (Int i = 0; i & lt; a.Length; i ++) if (A [I]! = B [I]) return; Back true; }
A more sophisticated approach (through Google) can be seen.
- What is the easiest way to compare two methods (using less code, but readable) net array?
- What is the most effective way to compare two NAT arrays?
Cathy's attitude feels good to me, I personally think the comparisonist Allow to specify:
bool areEqual & lt; T & gt; (T [] A, T [] B) {Return AreEqual (a, b, EqualityComparer & lt; T & gt; chuck); } Bool areEqual & lt; T & gt; (T [] A, T [B], IQuality Comparper Comparator) {// Comparison of handle identification, if compared to nulls (a == b) {true; } If (a == faucet || B == zero) {return false; } If (a.Length! = B.Length) {return false; } For (int i = 0; i & lt; a.Length; i ++) {if (compare !!) (a [i], b [ii])) {return false; True} true; }
The sequence, as mentioned by CMS, is good, but IEnumerable & lt; T & gt; I do not think it is "starting" if the length is not equal (it is possible that it checks for both sycounds, though, although it is possible to check the calculation directly). You can call the
IList & lt; T & gt;
Using bool can make a little more normal for AreEqual & lt; T & gt; (IList & lt; T & gt; A, ILIIT & lt; T & gt; B, IQ quality compairer & lt; T & gt; Comparative) {If (a.Count! = B.Count) {return false; } For (int i = 0; i & lt; a.Qount; i ++) {if (comparison! Exolls (a [ii], b [i])) {return false; True} true; } The direct array version will probably be the most efficient - adding generality and abstraction usually affects performance, even if it important depends on your app. Will do
Comments
Post a Comment