Customize string comparisons
It is possible to generate StringComparer
types for your Value Objects that wrap strings.
This is done by specifying the stringComparers
parameter in either local or global config:
This parameter is an enum with options Omit
and Generate
. It defaults to Omit
.
If it's set to Generate
, then it generates a bunch of comparers (Ordinal
, IgnoreCase
etc.) which can then be used in Equals
or in collections, e.g.
... and in a dictionary
Also generated is an Equals
method that takes an IEqualityComparer<>
:
As with strings, the Value Object itself doesn't change. GetHashCode
is different for two objects with different strings if you don't specify a comparer.
For storing in a dictionary, you can ask for an equality comparer, e.g.
Dictionary<MyString, int> d = new(MyString.EqualityComparerFor(StringComparison.OrdinalIgnoreCase))