C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-15-2006, 01:36 AM   #1
Registered Viking
 
Join Date: Aug 2006
Location: Norway
Posts: 19
Comparing values of generic types

Hi all!

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;
    }
}
Now, this function doesn't work, and I quess it's because <T> COULD be a reference type, or a value type. Is there a way to allow only value-types as <T>?

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   Reply With Quote
Old 10-15-2006, 08:51 AM   #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   Reply With Quote
Old 10-15-2006, 09:25 AM   #3
Registered Viking
 
Join Date: Aug 2006
Location: Norway
Posts: 19
Quote:
Originally Posted by pianorain
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.
I know, but consider this:
Code:
bool someMethod<T>(T a, T b)
{
    if ( a.Equals(b) )
    {
        Console.Writeline("Hooray!");
        return true;
    }
    return false;
}
This will not work so long as your dealing with generic types.
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   Reply With Quote
Old 11-27-2006, 08:24 PM   #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   Reply With Quote
Old 11-28-2006, 08:36 AM   #5
Anti-Poster
 
Join Date: Feb 2002
Posts: 1,241
Quote:
Originally Posted by MisterT
This will not work so long as your dealing with generic types.
Please define what you mean by "this will not work," because this appears to work just fine:
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();
}
Output:
Code:
Whoops!
Hooray!
Whoops!
Finished
__________________
Rule #1: Every rule has exceptions
pianorain is offline   Reply With Quote
Old 11-28-2006, 03:20 PM   #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   Reply With Quote
Old 11-29-2006, 09:17 AM   #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   Reply With Quote
Old 11-29-2006, 03:04 PM   #8
Registered User
 
Join Date: Aug 2004
Posts: 731
Okay thanks.
Rune Hunter is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:30 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22