Jump to content
  • Advertisement
Sign in to follow this  
NexusOne

[.net] Why can't I say if(struct1 == struct2) in C#?

This topic is 4879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
You can say if(struct1 == struct2). THey are both value types so its legal but u cant say class1 == class2.

Share this post


Link to post
Share on other sites
You have to override the == operator, and if you're going to do that, it'd make sense to override the Equals() function as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by skittleo
In C++ at least, structs and classes are basically the same thing, except that structs' members are by default public whereas classes' members are by default private. I would expect the same to be true in C#. You can always overload the == operator.
In C# structs and classes are different. structs are allocated on the stack, whereas classes are allocated on the heap. Here's a more detailed description of the differences.

Share this post


Link to post
Share on other sites
But overriding .Equals is just syntactic sugar, it's not any faster than calling .Equals, and all I care about is performance in this particular app. I don't want the overhead of a .Equals method call.

Share this post


Link to post
Share on other sites
Override the Equals method to compare your structs however you want, and then define a == operator which calls the Equals method. The JIT will almost certainly inline the == operator method, and if your Equals method is simple enough, probably that too.

Share this post


Link to post
Share on other sites
Quote:
Original post by NexusOne
But overriding .Equals is just syntactic sugar, it's not any faster than calling .Equals, and all I care about is performance in this particular app. I don't want the overhead of a .Equals method call.


If you were going to brand anything as "syntactic sugar", I would probably aim that comment at operator overloads, rather than the overriding of a virtual method, but hey...

Quote:
Original post by Holy Fuzz
Override the Equals method to compare your structs however you want, and then define a == operator which calls the Equals method. The JIT will almost certainly inline the == operator method, and if your Equals method is simple enough, probably that too.


Actually, wouldn't it be better to implement this the other way around (i.e. have the member comparison in the == operator, and call this inside Equals), as a call to Equals would involve boxing/unboxing, whereas the body of the == operator method would have both objects typed as value-types?

Share this post


Link to post
Share on other sites
Quote:
Actually, wouldn't it be better to implement this the other way around (i.e. have the member comparison in the == operator, and call this inside Equals), as a call to Equals would involve boxing/unboxing, whereas the body of the == operator method would have both objects typed as value-types?


This is true. What I like to do is provide a custom, overloaded MyType.Equals(MyType obj) method, which is called by both the overriden Equals(object obj) method and the == operator. That way, there aren't any unboxing issues, and my personal preference is to not put any "meat" inside of operator methods, but instead to merely provide them as syntactic shortcuts to the methods that do the real work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Holy Fuzz
This is true. What I like to do is provide a custom, overloaded MyType.Equals(MyType obj) method, which is called by both the overriden Equals(object obj) method and the == operator. That way, there aren't any unboxing issues, and my personal preference is to not put any "meat" inside of operator methods, but instead to merely provide them as syntactic shortcuts to the methods that do the real work.


Good call... this is a wise way to do it in order to make your structs/classes CLS-compliant (as operator overloads are only available in C# for .NET 1.x, while both C# and VB.NET get the feature in 2.0), just in case some twisted individual wants to use your code from COBOL.NET or something equally freaky and wrong ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!