Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. WHY THIS CODE RETURNS ERROR

WHY THIS CODE RETURNS ERROR

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RINSON VARGHESE
    wrote on last edited by
    #1

    public class Comparer<T> { public static bool AreEqual(T o1, T o2) { return (o1 == o2); } } //problem is with O1 == o2 //Help me? //URGENT

    C D 2 Replies Last reply
    0
    • R RINSON VARGHESE

      public class Comparer<T> { public static bool AreEqual(T o1, T o2) { return (o1 == o2); } } //problem is with O1 == o2 //Help me? //URGENT

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      because you've not specified that T needs to be an object that has an operator ==, I'd say. It's hard to tell for sure, given that you've not told us the error. I believe you can use .Equals, which is on the object class, but not ==. Again, if I'm wrong it's because you've given me nothing to go on. http://msdn.microsoft.com/en-us/library/d5x73970.aspx[^] is an article on constraints in generics.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      R 1 Reply Last reply
      0
      • C Christian Graus

        because you've not specified that T needs to be an object that has an operator ==, I'd say. It's hard to tell for sure, given that you've not told us the error. I believe you can use .Equals, which is on the object class, but not ==. Again, if I'm wrong it's because you've given me nothing to go on. http://msdn.microsoft.com/en-us/library/d5x73970.aspx[^] is an article on constraints in generics.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        R Offline
        R Offline
        RINSON VARGHESE
        wrote on last edited by
        #3

        i think ...what u said is correct.... ..the error i am getting is "operator == can not be applied to operands of type T and T" thanks for ur reply .....

        1 Reply Last reply
        0
        • R RINSON VARGHESE

          public class Comparer<T> { public static bool AreEqual(T o1, T o2) { return (o1 == o2); } } //problem is with O1 == o2 //Help me? //URGENT

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          The reason is not everything implements the == operator. All classes (reference types) do, but not all structs (value type) do. If you know that T is always going to be a class then you can use

          public class Comparer<T> where T: class
          {
          // ...
          }

          and your code will work. All objects implement Equals(object obj) so you can use o1.Equals(o2), but it may not give the result you expect depending on the struct's/class' implementation of this method.

          Dave
          Generic BackgroundWorker - My latest article!
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups