343 questions
0
votes
1
answer
64
views
Why is my Dictionary<ValidationTarget, List<ValidationDirective>> failing here?
This is puzzling me greatly. This tiny bit of test code is failing with a KeyNotFoundException.
foreach (var key in this.Keys)
{
List<ValidationDirective> test = this[key];
}
It seems ...
1
vote
2
answers
114
views
Why does String.GetHashCode have O(1) complexity
I want to use string as a key type in a Dictionary. Like Dictionary<string, string>.
As I understand, if I want to Add a new object, it first calculate a hashcode of the key. So the complexity ...
3
votes
2
answers
744
views
Get hashcode of a HashSet based on the items present in the HashSet
I have a HashSet<T> and I want to get the hashcode of the HashSet<T> based on the items it contains. I am trying to identify a HashSet<T> based on it's values. I know I can use set ...
0
votes
1
answer
132
views
Distinct is not working with IEquatable<T>
I have a class Bar that looks like this:
public class Bar : IEquatable<Bar>
{
public string Stringbar1{ get; set; }
public string Stringbar2{ get; set; }
public string Stringbar3{ ...
42
votes
2
answers
5k
views
Why do string hash codes change for each execution in .NET?
Consider the following code:
Console.WriteLine("Hello, World!".GetHashCode());
First run:
139068974
Second run:
-263623806
Now consider the same thing written in Kotlin:
println("...
2
votes
1
answer
158
views
How to find a string with specific letters in gethash javascript function
function getHash( string ) {
let h = 7, letters = "acdefhlmnoprstuw";
for( var i = 0; i < string.length; i++ ) {
h = ( h * 37 + letters.indexOf( string[ i ] ) );
} ...
0
votes
0
answers
42
views
How can i fix linkedlist using with iterator in Java?
When create a linkedlist in hash class search method, my iterator does not work with it. What can i use instead of iterator? How can i fix it.
public static void search(String keyword){
String ...
0
votes
0
answers
33
views
Is it safe to implement GetHashCode by returning an unique ID from the object?
Let's say I have a POCO that represents a database row. I have access to the ID of that row.
Is it safe to implement GetHashCode and Equals by leveraging that unique ID ?
public class Project
{
...
2
votes
2
answers
118
views
GetHashCode for a pair of playing cards C#
I have a playing card class with the GetHashCode method implemented as follows:
public override int GetHashCode()
{
return (int)rank * 4 + (int)suit;
}
I now want to design a GetHashCode method ...
1
vote
1
answer
632
views
When overriding GetHashCode() should you also factor in the base class's HashCode? [closed]
class A
{
public int Age{get;set;}
public override GetHashCode()
{
return Age.GetHashCode();
}
}
class B : A
{
public int Name{get;set;}
public override GetHashCode()
...
2
votes
2
answers
337
views
How to implement the override methods of Equals and GetHashCode in a class that implements IProxyGenerationHook in Castle.Core?
Reading the Castle.Core documentation, in this link, they recommend that always overriding the Equals and GetHashCode methods of the classes that implement IProxyGenerationHook.
I have a class called ...
1
vote
0
answers
159
views
Is there a way in .NET Core to use the .NET Framework's implementation of GetHashCode()?
I am using a JWT token's hash as part of a cache key, since I want to provide different values when a different token is used.
The hash code is calculated in service written in .NET Framework, and is ...
0
votes
1
answer
288
views
Linq Group by Complex Object (E.G list element of object)
I have an object like below;
var json = @"{
'TableNumber': '3',
'TableId': '81872d39-9480-4d2d-abfc-b8e4f33a43b6',
'tableMenuItems': [
{
'Id': '4664a2d3-c0af-443d-8af5-...
0
votes
1
answer
164
views
Computed hashcode to lie within a particular range of values?
I have a function which looks like this:
public int getHashcode(int shardCount){
String personid = "<some long string value>";
String servicedate = "2019-12-22T01:31:30.000Z";
...
-1
votes
1
answer
91
views
I'd like to know how can I write good GetHashCode() for ordered list [duplicate]
I made a class that include List<T>.(T is a struct I made.) And I want to override Equals() and GetHashCode(). So I need to make HashCode from List<T>. How can I make good HashCode from ...
1
vote
0
answers
31
views
WPF Binding not refreshing if Equals and GetHashCode [duplicate]
My WPF binding is not working because my viewmodel has overwritten Equals and GetHashCode. If i comment it out (region Object-Stuff), everything works fine.
Further info about my reasons:
All my ...
1
vote
1
answer
511
views
Can I use GetHashCode without Equals?
Maybe I don’t understand something. No, I definitely don’t understand something. There is a custom class with two int type properties. Overriding the GetHashCode method, I want to use both of these ...
0
votes
2
answers
228
views
Implement a custom hashcode on a third party class
I'm using a Line class in VisualStudio C# project from a third party (no access to the source)
My code is generating hundreds/thousands of line objects containing duplicates and I need to store them ...
0
votes
1
answer
2k
views
What is equivalent for Java Objects.hash and Objects.hashCode in C#
Moving from Java to C# developer. In Java, I used a lot of Objects.hash(array) and Objects.hashCode(object) to build hash code of an object in hashCode function. I can't find any equivalent for those ...
1
vote
1
answer
484
views
Null vs Empty Collections in GetHashCode
In the implementation of GetHashCode below, when Collection is null or empty will both result in a hash code of 0.
A colleague suggested return a random hard coded number like 19 to differentiate ...
5
votes
2
answers
2k
views
Why does VS 2017 generate GetHashCode without an unchecked block
I recently discovered that Visual Studio 2017 can auto-generate overrides for Equals and GetHashCode, but I was wondering why the GetHashCode implementation is not in an unchecked block?
I made a ...
1
vote
3
answers
873
views
GetHashCode returns the same value for different objects. Is there any method to identify object by particular properties?
I'm trying to create a hashcode method. I have code like below :
private static object GetValue<T>(object item, string propertyName)
{
ParameterExpression arg = Expression....
0
votes
0
answers
66
views
HashSet<T> EqualityComparer that returns equal if SetEquals: which GetHashCode? [duplicate]
Seems like How does HashSet Compare elements for equality, but different, because that question focusses on the IEqualityComparer used by the HashSet. I want to write an Equality Comparer that ...
1
vote
2
answers
202
views
Implement GetHashCode for IRevertibleChangeTracking class
My current project uses Dapper to build a class based on rows from a database.
I've cut down my class to focus on the equality components of my question so please let me know if any additional code ...
2
votes
0
answers
59
views
Explanation for nested decimals behaviour in ValueType GetHashCode/Equals implementation
I was scratching my head over an unexpected failing comparison between two instances of a custom struct. I hope someone can either point me to reference source or documentation, or confirm whether ...