gethashcode

Is the .Net HashSet uniqueness calculation completely based on Hash Codes?

限于喜欢 提交于 2019-12-06 18:47:28
问题 I was wondering whether the .Net HashSet<T> is based completely on hash codes or whether it uses equality as well? I have a particular class that I may potentially instantiate millions of instances of and there is a reasonable chance that some hash codes will collide at that point. I'm considering using HashSet's to store some instances of this class and am wondering if it's actually worth doing - if the uniqueness of an element is only determined on its hash code then that's of no use to me

GetHashCode on null fields?

谁说胖子不能爱 提交于 2019-12-06 17:01:13
问题 How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public Address As String Public Overloads Function Equals(ByVal other As Contact) As Boolean _ Implements System.IEquatable(Of Contact).Equals Return Name = other.Name AndAlso Address = other.Address End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If

Why are these hashcodes equals?

Deadly 提交于 2019-12-06 03:19:06
This test is failing : var hashCode = new { CustomerId = 3354, ServiceId = 3, CmsThematicId = (int?)605, StartDate = (DateTime?)new DateTime(2013, 1, 5), EndDate = (DateTime?)new DateTime(2013, 1, 6) }.GetHashCode(); var hashCode2 = new { CustomerId = 1210, ServiceId = 3, CmsThematicId = (int?)591, StartDate = (DateTime?)new DateTime(2013, 3, 31), EndDate = (DateTime?)new DateTime(2013, 4, 1) }.GetHashCode(); Assert.AreNotEqual(hashCode, hashCode2); Can you tell me why ? Samuel Neff It's kinda amazing you found this coincidence. Anonymous classes have a generated GetHashCode() method that

GetHashCode and Equals implementation in Dictionary C#

吃可爱长大的小学妹 提交于 2019-12-05 22:13:03
I came to this site searching for object comparison in Dictionary, and i came to know that overriding GetHashCode and Equals are a must for doing object comparison in C#. Here is a piece of code that i have been trying to solve out, using FOREACH iteration Method. But my Boss says to do the same without using any iteration(maybe by using containskey or containsvalue method), due to performance issues. Any help is highly welcome.. public class employee { public string empname { get; set; } public string location { get; set; } public double kinid { get; set; } public double managerKin { get; set

Is the .Net HashSet uniqueness calculation completely based on Hash Codes?

痞子三分冷 提交于 2019-12-05 00:15:14
I was wondering whether the .Net HashSet<T> is based completely on hash codes or whether it uses equality as well? I have a particular class that I may potentially instantiate millions of instances of and there is a reasonable chance that some hash codes will collide at that point. I'm considering using HashSet's to store some instances of this class and am wondering if it's actually worth doing - if the uniqueness of an element is only determined on its hash code then that's of no use to me for real applications MSDN documentation seems to be rather vague on this topic - any enlightenment

C# how to calculate hashcode from an object reference

Deadly 提交于 2019-12-04 23:22:21
Folks, here's a thorny problem for you! A part of the TickZoom system must collect instances of every type of object into a Dictionary<> type. It is imperative that their equality and hash code be based on the instance of the object which means reference equality instead of value equality. The challenge is that some of the objects in the system have overridden Equals() and GetHashCode() for use as value equality and their internal values will change over time. That means that their Equals and GetHashCode are useless. How to solve this generically rather than intrusively? So far, We created a

GetHashCode on null fields?

一曲冷凌霜 提交于 2019-12-04 22:35:40
How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public Address As String Public Overloads Function Equals(ByVal other As Contact) As Boolean _ Implements System.IEquatable(Of Contact).Equals Return Name = other.Name AndAlso Address = other.Address End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If ReferenceEquals(Me, obj) Then Return True If TypeOf obj Is Contact Then Return Equals(DirectCast(obj,

C#: How would you unit test GetHashCode?

馋奶兔 提交于 2019-12-04 09:55:08
问题 Testing the Equals method is pretty much straight forward (as far as I know). But how on earth do you test the GetHashCode method? 回答1: Test that two distinct objects which are equal have the same hash code (for various values). Check that non-equal objects give different hash codes, varying one aspect/property at a time. While the hash codes don't have to be different, you'd be really unlucky to pick different values for properties which happen to give the same hash code unless you've got a

Is .GetHashCode guaranteed to be the same across systems/platform versions? [duplicate]

荒凉一梦 提交于 2019-12-04 06:26:53
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Can I depend on the values of GetHashCode() to be consistent? If I use the Object.GetHashCode() method across two systems/framework versions, am I guaranteed to get the same value for the same input? In other words, does its value make a good key for persistent data? Note: I don't care about collisions in this problem. As a bonus, am I guaranteed to get the same value in Mono vs. Microsoft .Net? 回答1: No. Other

Have I implemented Equals()/GetHashCode() correctly?

你说的曾经没有我的故事 提交于 2019-12-04 03:25:14
The program was working with this implementation: class Instrument { public string ClassCode { get; set; } public string Ticker { get; set; } public override string ToString() { return " ClassCode: " + ClassCode + " Ticker: " + Ticker + '.'; } } But because I need to use Instrument in Dictionary I've decided to implement equals/hashcode: class Instrument { public string ClassCode { get; set; } public string Ticker { get; set; } public override string ToString() { return " ClassCode: " + ClassCode + " Ticker: " + Ticker + '.'; } public override bool Equals(object obj) { if (obj == null) return