{"id":20605208,"url":"https://github.com/arnab-developer/gethashcode-comparison","last_synced_at":"2026-04-18T22:03:04.259Z","repository":{"id":68915174,"uuid":"411734767","full_name":"Arnab-Developer/gethashcode-comparison","owner":"Arnab-Developer","description":"GetHashCode comparison","archived":false,"fork":false,"pushed_at":"2022-02-24T03:42:19.000Z","size":685,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-17T02:24:19.142Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Arnab-Developer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-29T15:44:29.000Z","updated_at":"2021-12-05T05:48:17.000Z","dependencies_parsed_at":"2023-03-24T11:18:32.595Z","dependency_job_id":null,"html_url":"https://github.com/Arnab-Developer/gethashcode-comparison","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2Fgethashcode-comparison","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2Fgethashcode-comparison/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2Fgethashcode-comparison/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnab-Developer%2Fgethashcode-comparison/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Arnab-Developer","download_url":"https://codeload.github.com/Arnab-Developer/gethashcode-comparison/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242250932,"owners_count":20096895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T09:27:07.428Z","updated_at":"2026-04-18T22:02:59.215Z","avatar_url":"https://github.com/Arnab-Developer.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GetHashCode comparison\n\nThis is a comparison of `Object.GetHashCode()` value between `class` and `record`.\n\nIf you have a class\n\n```csharp\nclass StudentClass\n{\n    public int Id { get; set; }\n    public string FirstName { get; set; } = string.Empty;\n    public string LastName { get; set; } = string.Empty;\n}\n```\n\nand you have created an object of that class\n\n```csharp\nStudentClass s1 = new() { Id = 1, FirstName = \"Jon\", LastName = \"Doe\" };\n```\n\nNow if you called `GetHashCode()` on it multiple times you will get same values. If\nyou restart the app, you will get same values again.\n\n```csharp\nWriteLine(s1.GetHashCode());\nWriteLine(s1.GetHashCode());\nWriteLine(s1.GetHashCode());\nWriteLine(s1.GetHashCode());\n```\n\nBut if you do the same thing with `record` you will get same values for same object but\nif you restart the app, you will get different set of values.\n\nThe previous test has been done with a .NET 5 console app. If I do the same thing with ASP.NET 5 then the `Object.GetHashCode()` \nbehaviour is opposite. There `Object.GetHashCode()` returns different values for every request\nfor the `class` objects but same for the `record` objects.\n\nI did same test with a `string` value and found that it returns same value if the application is running for every requests.\nBut if I restart the app then it returns different value. So for `string` `Object.GetHashCode()` return same value\nuntil the app domain is restarted.\n\nSo the conclusion is never store return value of `Object.GetHashCode()` out of the application to\ncheck the object equality. If you need to store the value of `Object.GetHashCode()` out of the application and use that in future\nthen override `GetHashCode()` in your class.\n\n\u003e Furthermore, .NET does not guarantee the default implementation of the GetHashCode method, and the value \n\u003e this method returns may differ between .NET implementations, such as different versions of .NET Framework \n\u003e and .NET Core, and platforms, such as 32-bit and 64-bit platforms. For these reasons, do not use the default \n\u003e implementation of this method as a unique object identifier for hashing purposes. Two consequences follow \n\u003e from this:\n\u003e\n\u003e - You should not assume that equal hash codes imply object equality.\n\u003e - You should never persist or use a hash code outside the application domain in which it was created, because the same object may hash across application domains, processes, and platforms.\n\nSee this [document](https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=net-5.0#remarks)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnab-developer%2Fgethashcode-comparison","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farnab-developer%2Fgethashcode-comparison","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnab-developer%2Fgethashcode-comparison/lists"}