Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by teh1archon · Aug 08, 2016 at 10:20 PM · c#valuestructlinqhash

Strange behavior using structs with HashSet

Hello,

I've made the following struct to be used in a HashSet (the normal using System.Collections.Generic + System.Linq). It's struct rather than a class so when I make new stucts (as demonstrated below) so when I check equality and if the set contains it could pass items by value rather than by reference (which will always won't give me the result I need).

The struct:

 public struct BoardCell
 {
     public int column;
     public int row;
     public int board;
 
     public BoardCell(int c, int r, int b)
     {
         column = c;
         row = r;
         board = b;
     }
 
     public override bool Equals(object obj)
     {
         if (!(obj is BoardCell))
             return false;
         return (this.column == ((BoardCell)obj).column && this.row == ((BoardCell)obj).row && this.board == ((BoardCell)obj).board);
     }
 
     public static bool operator ==(BoardCell l, BoardCell r)
     {
         return (l.column == r.column && l.row == r.row && l.board == r.board);
     }
 
     public static bool operator !=(BoardCell l, BoardCell r)
     {
         return !(l == r);
     }
 
     public override int GetHashCode()
     {
         return (column * 10000 + row * 100 + board);
     }
 
     public static BoardCell GetReversed(BoardCell bc, int gridSize)
     {
         return new BoardCell(gridSize - bc.column - 1, gridSize - bc.board - 1, gridSize - bc.row - 1);
     }
 
     public static BoardCell GetInvalid()
     {
         return new BoardCell(-1, -1, -1);
 
     }
 
     public static bool IsValid(BoardCell bc)
     {
         return (bc.column != -1);
     }
 }
 
 And the code that checks if an instance of this struct is in the set:
 
     public void UpdateCell(int column, int row, int board, bool value)
     {
         gameBoard[column, row, board].OccupiedBy = value;
         BoardCell bc = new BoardCell(column, row, board);
             print(freeCells.Contains(bc));
             if (!freeCells.Remove(bc))
                 Debug.LogError("duplicate?!");
         
         LastCell = new BoardCell(column, row, board);
         CheckWinner(column, row, board);
     }

When I put prints in there there was something peculiar: The Remove method would ALWAYS return false as in the object is not in the set yet it did find it and remove it. The Contains method also returns false all the time. Now for what I know I didn't even had to override and implement equals, operator == and != and GetHashCode but since it didn't work right I've done it anyways and it still returns false upon check yet removes the correct struct from the set. Also it f***s up elsewhere where I query the set with Linq-SQL syntax, failing basic basic (if a.x == b.x return true) kind of stuff.

Is it a bug? Am I missing something? Using 5.4 and I've never had these kind of problems with 5.1 (the last time I've used Linq in Unity).

Best regards, Shay.

Comment
Add comment · Show 1
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 Tomer-Barkan · Aug 09, 2016 at 11:01 AM 0
Share

Share the rest of your code, there's nothing about a HashSet here.

Anyway these are part of the mono/C# infrastructure not part of Unity, I find it very hard to believe there's such a basic bug in those.

0 Replies

· Add your reply
  • Sort: 

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

215 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 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 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 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 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 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 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 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 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 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

List of Structs variable values not changing 1 Answer

How to use SyncListStruct properly? 0 Answers

UI image component problems with Character Creation.... 0 Answers

Disable hashing of function names when printing stacktrace to file in unity build 0 Answers

Animator.GetFloat returning wrong value unity 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