![]() |
| | #1 |
| Registered Viking Join Date: Aug 2006 Location: Norway
Posts: 19
| Comparing values of generic types Consider the following sample: Code: public abstract class SomeClass
{
// checks equality of each member in two arrays.
public static bool ArrayEquals<T>(T[] a, T[] b)
{
if (a.Length != b.Length) {
return false;
}
for (int i = 0; i < a.Length; i++) {
if ( a[i] != b[i] ) {
return false;
}
}
return true;
}
}
Or, alternately, is there another way of checking the value(not reference) of each entity in a and b? object.Equals(a[i], b[i]) does not work because it checks the ref. Thanks! Last edited by MisterT; 10-15-2006 at 01:39 AM. |
| MisterT is offline | |
| | #2 |
| Anti-Poster Join Date: Feb 2002
Posts: 1,241
| In my opinion, Equals is the correct way to write this function. Each class can override Equals to check the actual value of the class if it makes sense. However, instead of using the static object.Equals, you should use a[i].Equals(b[i]). If you want to restrict to value types, I think you can use the struct generic type constraint. However, the Equals method is better because then you can use it on both value types and atomic reference types.
__________________ Rule #1: Every rule has exceptions |
| pianorain is offline | |
| | #3 | |
| Registered Viking Join Date: Aug 2006 Location: Norway
Posts: 19
| Quote:
Code: bool someMethod<T>(T a, T b)
{
if ( a.Equals(b) )
{
Console.Writeline("Hooray!");
return true;
}
return false;
}
I do belive it's because with gens it always checks the ref instead of the potential value, even thou it's a value type. It's not a big dealie, cus I've overridden the method to support the datatypes I need it to, without generics, but I just found the topic interesting. | |
| MisterT is offline | |
| | #4 |
| Registered User Join Date: Aug 2004
Posts: 731
| So how does the List<T> class do it? Does it use obj.Equals as well? |
| Rune Hunter is offline | |
| | #5 | |
| Anti-Poster Join Date: Feb 2002
Posts: 1,241
| Quote:
Code: class A : IEquatable<A>
{
int m_int;
public A(int a) { m_int = a; }
public override bool Equals(object obj)
{
A other = obj as A;
if (other == null)
return false;
return Equals(other);
}
public bool Equals(A other)
{
return m_int == other.m_int;
}
}
static bool someMethod<T>(T a, T b)
{
if (a.Equals(b))
{
Console.WriteLine("Hooray!");
return true;
}
Console.WriteLine("Whoops!");
return false;
}
[STAThread]
static void Main(string[] args)
{
A a = new A(3);
A b = new A(4);
A a2 = new A(3);
someMethod(a, b);
someMethod(a, a2);
someMethod(a2, b);
Console.WriteLine("Finished");
Console.ReadLine();
}
Code: Whoops! Hooray! Whoops! Finished
__________________ Rule #1: Every rule has exceptions | |
| pianorain is offline | |
| | #6 |
| Registered User Join Date: Aug 2004
Posts: 731
| Ok so I'm guessing that List<T> does use object.Equals. Also for types like int, float, string are they checked for there value rather than there reference? |
| Rune Hunter is offline | |
| | #7 |
| Anti-Poster Join Date: Feb 2002
Posts: 1,241
| Yes, I'm sure any of the built-in data types that require some sort of equality functionality uses object.Equals. For value types and string, the default Equals will automatically do a value comparison.
__________________ Rule #1: Every rule has exceptions |
| pianorain is offline | |
| | #8 |
| Registered User Join Date: Aug 2004
Posts: 731
| Okay thanks. |
| Rune Hunter is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| disposing error | dropper166 | C# Programming | 2 | 03-30-2009 11:53 PM |
| Comparing adjacent values in a file | typhonius | C Programming | 10 | 02-10-2009 03:41 AM |
| putting values into an array | zdream8 | C Programming | 15 | 05-21-2008 11:18 PM |
| Computing Large Values | swbluto | C++ Programming | 8 | 04-07-2005 03:04 AM |
| i fail to call values of structure variable in another function | mrprogrmr | C Programming | 1 | 02-03-2005 03:33 AM |