Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by The_Magical_Kiwi · May 21, 2014 at 07:11 PM · c#referencevalue

c# How does an == decide if two objects are the same?

Hey guys,

I've had a bit of a google around for the answer to this question, maybe I don't know the terms I need in order to find the answer so I'm going to ask on here. Please forgive me if this has been answered somewhere before.

I know that most things are passed around by reference in unity [[source]][1] so I wonder if it is the memory address that is compared; that way it would return true if the two references referred to the same instance of a class. Perhaps (as I do) you might want to compare the values of an object, would comparing two instances of a class return true if they were different instances but had the same values?

I realise I could test both of these cases but I'm interested in the logic that lies behind a comparison so I can make a more informed decision about comparisons when I'm coding.

Thanks in advance for any help you can provide me.

[1]: http://answers.unity3d.com/questions/225286/how-do-i-know-whether-variable-is-pass-by-value-or.html

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Jeff-Kesselman · May 21, 2014 at 07:37 PM

The Object class has a virtual method called Equals.

BY default, that method calls a non-virtual method called ReferenceEquals. You can read about ReferenceEquals here but yes, basically, it checks the location in memory. http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

HOWEVER any class can override Equals and many do. So you need to check the documentation for the class in question.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The_Magical_Kiwi · May 21, 2014 at 08:12 PM 0
Share

So if I wanted to compare my class by value, I could override with my own equals method in order to do so?

avatar image DMGregory · May 21, 2014 at 08:30 PM 2
Share

Be careful here - Unity also overrides the equality operator for anything derived from UnityEngine.Object (including ScriptableObject, GameObject, and $$anonymous$$onoBehaviour) - this overload causes destroyed objects to compare as equal to null until they're actually disposed.

If you overload equals, you may interfere with this behaviour.

If you need a custom comparison for a subclass of UnityEngine.Object, it may be safer to define your own IsIdenticalTo(other) method or something similar. That way you can be explicit about when your comparison should be used ins$$anonymous$$d of the usual behaviour.

avatar image The_Magical_Kiwi · May 22, 2014 at 12:54 PM 0
Share

Interesting, okay, given that writing my own is no more difficult, I'll just do that. Thanks for the advice.

avatar image Jeff-Kesselman · May 23, 2014 at 05:29 PM 1
Share

The other issue with overriding equals to be value based to be aware of is that many collections use it to deter$$anonymous$$e equality so an override may produce unwanted collection results. For instance a Set would only allow one object with any given value. If used as a key in a Dictionary, it would match on value not on object which, again, may not be what you want. If you call Remove(object) it will remove the first object with the same value, etc..

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Store reference to array as variable 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can you store a reference to a primitive value? 1 Answer

[c#] Save an instance for future re-instantiation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges