Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plain Old Class Object not returning Unique Hash Code #892

Closed
alberk8 opened this issue Nov 19, 2021 · 2 comments · Fixed by nanoframework/nf-interpreter#2157
Closed

Plain Old Class Object not returning Unique Hash Code #892

alberk8 opened this issue Nov 19, 2021 · 2 comments · Fixed by nanoframework/nf-interpreter#2157

Comments

@alberk8
Copy link

alberk8 commented Nov 19, 2021

Details about Problem

Plain Old Class Objects (POCO) are not returning unique hash code.

Target:
ESP32

Firmware image version:
1.7.1-preview.1125

Worked before? If so, with which nanoFramework image version:
First time encountering the issue.

Description

The POCO are returning zero or negative int hash code. Also when multiple instance are created.

Detailed repro steps so we can see the same problem

public class Program
{
	public static void Main()
	{
		var a = new TestClass();
		var b = new TestClass();
		
		Debug.WriteLine($"HashCode A {a.GetHashCode()} HashCode B {b.GetHashCode()}");
		
		var c = new NewClass();
		var d = new NewClass();
		
		Debug.WriteLine($"HashCode C {c.GetHashCode()} HashCode D {d.GetHashCode()}");
	}
	
}

\\ place in a separate class file, lets call Models.cs 
 public class TestClass
    {
        private int _int;
        public TestClass()
        {
            Debug.WriteLine($"TestClass Constructor HashCode {GetHashCode()}");
            // if _int is moved aboved the Debug line then the GetHashCode will return Value
            _int = 100;
        }

        public void DoSomething()
        {
            _int = 1 + 2;
        }
    }

    public class NewClass
    {
        public string Name { get; set; }
        public string Address { get; set; }
    }

Other suggested things

Expected behaviour

Expect to get unique hash code for every instance of the class and do not return zero value..

Additional context

Add any other context about the problem here.

\\ Debug Output
TestClass Constructor HashCode 0
TestClass Constructor HashCode 0
HashCode A 1192204961 HashCode B 1192204961
HashCode C 0 HashCode D 0

Make an effort to fix the bug

Attempted but could not get to the root cause.

@alberk8
Copy link
Author

alberk8 commented Nov 20, 2021

After further testing. I manage to find two patterns for Hash code == 0. This is when property where get and set are defined without any logic or private fields

private int _int;
// or
public string Name { get; set; }

@josesimoes
Copy link
Member

Cross check for .NET 6.0: https://dotnetfiddle.net/FWy25q

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants