Skip to content

Informatievlaanderen/hashcode-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Be.Vlaanderen.Basisregisters.HashCodeCalculator Build Status

Goal

Calculates hashcode based on given fields. This allows you to implement class identity.

Usage

When running these examples, keep in mind GetHashCode will not return the same for each run!

The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across .NET implementations, across .NET versions, and across .NET platforms (such as 32-bit and 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes.

As a result, hash codes should never be used outside of the application domain in which they were created, they should never be used as key fields in a collection, and they should never be persisted.

Read String.GetHashCode for more information on this, and Why is string.GetHashCode() different each time I run my program in .NET Core? for more background information.

As a static helper

The most basic form of this library is to simply call it passing in the properties used for hashcode calculation.

namespace Example
{
    using System;
    using Be.Vlaanderen.Basisregisters.Utilities;

    public class Address
    {
        public string Address1 { get; set; }
        public string City { get; set; }
        public string State { get; set; }

        public override int GetHashCode()
            => HashCodeCalculator.GetHashCode(new[] { Address1, City, State });
    }

    public class Program
    {
        public static void Main(string[] _)
        {
            var test = new Address { Address1 = "Dummy Street 42", City = "Nowhere", State = "MN" };
            Console.WriteLine(test.GetHashCode());
        }
    }
}

Using yield return

You can also use this to pass in an IEnumerable which allows you to use yield return to supply the values for the hashcode calculation.

This allows you to stream values using yield.

namespace Example
{
    using System;
    using System.Collections.Generic;
    using Be.Vlaanderen.Basisregisters.Utilities;

    public class Address
    {
        public string Address1 { get; set; }
        public string City { get; set; }
        public string State { get; set; }

        private IEnumerable<object> HashCodeFields()
        {
            yield return Address1;
            yield return City;
            yield return State;
        }

        public override int GetHashCode()
            => HashCodeCalculator.GetHashCode(HashCodeFields);
    }

    public class Program
    {
        public static void Main(string[] _)
        {
            var test = new Address { Address1 = "Dummy Street 42", City = "Nowhere", State = "MN" };
            Console.WriteLine(test.GetHashCode());
        }
    }
}

As an extension method

There is also an extension method on T.

namespace Example
{
    using System;
    using Be.Vlaanderen.Basisregisters.Utilities;

    public class Address
    {
        public string Address1 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
    }

    public class Dummy
    {
        public void Example(Address test)
        {
            var s = test.GetHashCode(
                x => new object[] { x.Address1, x.City, x.State });

            Console.WriteLine(s);
        }
    }

    public class Program
    {
        public static void Main(string[] _)
        {
            var test = new Address { Address1 = "Dummy Street 42", City = "Nowhere", State = "MN" };
            var dummy = new Dummy();

            dummy.Example(test);
        }
    }
}

License

MIT License

Credits

Languages & Frameworks

  • .NET Core - MIT
  • .NET Core Runtime - CoreCLR is the runtime for .NET Core. It includes the garbage collector, JIT compiler, primitive data types and low-level classes. - MIT
  • .NET Core APIs - CoreFX is the foundational class libraries for .NET Core. It includes types for collections, file systems, console, JSON, XML, async and many others. - MIT
  • .NET Core SDK - Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI. - MIT
  • Roslyn and C# - The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs. - Apache License 2.0
  • F# - The F# Compiler, Core Library & Tools - MIT
  • F# and .NET Core - F# and .NET Core SDK working together. - MIT

Libraries

  • Paket - A dependency manager for .NET with support for NuGet packages and Git repositories. - MIT
  • FAKE - "FAKE - F# Make" is a cross platform build automation system. - MIT
  • xUnit - xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. - Apache License 2.0
  • Shouldly - Should testing for .NET - the way Asserting Should be! - BSD

Tooling

Flemish Government Libraries