Thread: Comparing values of generic types

  1. #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.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #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.

  4. #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?

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #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?

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Okay thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Comparing adjacent values in a file
    By typhonius in forum C Programming
    Replies: 10
    Last Post: 02-10-2009, 03:41 AM
  3. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  4. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM