CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2007
    Posts
    491

    Compare 2 generic types?

    Say I have a generic class with a generic type T, and in some point (which occurs after the instance of the class was created, i.e. the T type is now concrete) I want to compare 2 T's, like the following:
    Code:
            public bool Remove(T info)
            {
                if (this.Count == 0)
                    return false;
                if (head.Info == info)
                {
                    head = head.Next;
                    this.Count--;
                    return true;
                }
                //etc...
           }
    I get a precompiled error:
    Error 1 Operator '==' cannot be applied to operands of type 'T' and 'T' C:\Documents and Settings\Gerbi\Local Settings\Application Data\Temporary Projects\List\List.cs 33 17 MyList
    (I want to compare the address of the two T's in the memory and not their content)
    Is it possible to make such comparison? If so, what should I replace the following line to?
    Code:
                if (head.Info == info)

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Compare 2 generic types?

    You can use:
    Code:
    if(head.Info.Equals(info))
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Jan 2007
    Posts
    491

    Re: Compare 2 generic types?

    Well, I thought about it, but what if the concrete type doesn't have the Equals method overrided?
    Plus, I don't acually want to compare the content of the 2 objects. All I want to know is if the 2 objects refer to the same object in memory.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Compare 2 generic types?

    I think it is because == operator can be overriden.

    To compare references use Object.ReferenceEquals(). Using this method, you could be sure that no override method is used and the parameters are really compared by references.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Compare 2 generic types?

    Quote Originally Posted by boudino
    I think it is because == operator can be overriden.

    To compare references use Object.ReferenceEquals(). Using this method, you could be sure that no override method is used and the
    parameters are really compared by references.
    As Generic Types can be everything you cannot simple compare them using '==' What should be compared that way ? adress, type, properties of a class, or is it simple a valueType and you only want to compare the value itself.

    So if you want to compare in a generic method you need to restrict T and implement an interface like IEquatable<T>
    Code:
    public class combi<T,U> where T: IEquatable<U>{
       public bool test(T valT, U valU) {
    	  return valT.Equals(valU);
       }
    }
    
    This is the standard way to create generics. anyone who uses this generic needs to implement ICompareable to whatever his T is and then it will work.

    Comparing adresses, just as boudino told you you can do
    Code:
    public bool ItemTest(U val, T val1 ) {
    	return object.ReferenceEqualsval, val1);
    }
    I have tested this in an example, it compiles and it works independent if you compare value or reference types
    Last edited by JonnyPoet; August 29th, 2008 at 02:09 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    Jan 2007
    Posts
    491

    Re: Compare 2 generic types?

    Thank you guys a lot. Works great!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured