I've played around with this a bit over the years, and the most robust solution I've been able to come up with is to create a String
-like wrapper which does everything in a case-insensitive manner. Although depending on your needs, this may be over-kill ...
Usage is:
using StringOrdinalIgnoreCase = JDanielSmith.System.String<JDanielSmith.System.OrdinalIgnoreCase>;
[TestMethod]
public void StringOrdinalIgnoreCaseDictionary()
{
var d = new Dictionary<StringOrdinalIgnoreCase, int>() { { "abc", 1 }, { "def", 2 } };
Assert.IsTrue(d.ContainsKey("ABC"));
try
{
d.Add("DEF", 2);
Assert.Fail();
}
catch (ArgumentException) { }
}
String.cs
using System;
using CodeAnalysis = System.Diagnostics.CodeAnalysis;
namespace MyNS.System
{
/// <summary>
/// Provide a case-insensitive wrapper around System.String.
///
/// This is especially useful when using strings as keys in collections, where the key is something like a Windows file-system pathname;
/// it can be easy to forget to pass an IEqualityComparer<> in the constructor.
///
/// Some hints from: http://stackoverflow.com/questions/33039324/how-can-system-string-be-properly-wrapped-for-case-insensitivy
/// </summary>
[CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "String")]
[CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1036:OverrideMethodsOnComparableTypes")]
public sealed class String<TComparerAndComparison> : IComparable, ICloneable,
IComparable<String<TComparerAndComparison>>, IEquatable<String<TComparerAndComparison>>,
IComparable<String>, IEquatable<String>
where TComparerAndComparison : StringComparerAndComparison, new()
{
static readonly StringComparerAndComparison _comparerAndComparison = new TComparerAndComparison();
static readonly StringComparer _comparer = _comparerAndComparison.Comparer;
static readonly StringComparison _comparisonType = _comparerAndComparison.Comparison;
public string Value { get; }
public String(string value)
{
// matching the behavior of System.String is more straight-forward if "Value" is never null
Value = value ?? String.Empty;
}
// easily convert to/from System.String
public static implicit operator String<TComparerAndComparison>(string source) => new String<TComparerAndComparison>(source);
public static implicit operator string(String<TComparerAndComparison> source) => source?.Value;
#region Equals, IEquatable
public override bool Equals(object obj)
{
if (Object.ReferenceEquals(obj, null))
return false; // this != null
var other = obj as String<TComparerAndComparison>;
if (!Object.ReferenceEquals(other, null))
return Equals(other); // call Equals(String<TStringComparerAndComparison>)
var s_other = obj as string;
if (!Object.ReferenceEquals(s_other, null))
return Equals(s_other); // call Equals(string)
return _comparer.Equals(obj);
}
public bool Equals(String<TComparerAndComparison> other)
{
if (Object.ReferenceEquals(other, null))
return false; // this != null
return Equals(other.Value); // call Equals(string)
}
public bool Equals(string other) => _comparer.Equals(Value, other);
public override int GetHashCode()
{
return _comparer.GetHashCode(Value);
}
#endregion
public override string ToString() => Value;
public object Clone() => new String<TComparerAndComparison>(Value);
#region IComparable
public int CompareTo(object obj)
{
// https://msdn.microsoft.com/en-us/library/4d7sx9hd(v=vs.110).aspx
if (Object.ReferenceEquals(obj, null))
return 1; // If other is not a valid object reference, this instance is greater.
// obj must be either StringOrdinalIgnoreCase or String
var other = obj as String<TComparerAndComparison>;
if (Object.ReferenceEquals(other, null))
{
var s_other = obj as string;
if (Object.ReferenceEquals(s_other, null))
throw new ArgumentException("Object must be of type " + nameof(String<TComparerAndComparison>) + " or String.");
return CompareTo(s_other); // call CompareTo(string)
}
return CompareTo(other); // call CompareTo(StringOrdinalIgnoreCase)
}
public int CompareTo(String<TComparerAndComparison> other)
{
// https://msdn.microsoft.com/en-us/library/4d7sx9hd(v=vs.110).aspx
if (Object.ReferenceEquals(other, null))
return 1; // If other is not a valid object reference, this instance is greater.
if (Object.ReferenceEquals(Value, other.Value))
return 0;
return CompareTo(other.Value); // call CompareTo(string)
}
public int CompareTo(string other)
{
// https://msdn.microsoft.com/en-us/library/4d7sx9hd(v=vs.110).aspx
if (Object.ReferenceEquals(other, null))
return 1; // If other is not a valid object reference, this instance is greater.
return _comparer.Compare(Value, other);
}
public static bool operator ==(String<TComparerAndComparison> x, String<TComparerAndComparison> y)
{
if (Object.ReferenceEquals(x, null))
return Object.ReferenceEquals(y, null); // null == null, null != something
return x.Equals(y); // know x != null
}
public static bool operator ==(String<TComparerAndComparison> x, string y)
{
if (Object.ReferenceEquals(x, null))
return Object.ReferenceEquals(y, null); // null == null, null != something
return x.Equals(y); // know x != null
}
public static bool operator ==(string x, String<TComparerAndComparison> y) => y == x; // == is commutative, x == y
public static bool operator !=(String<TComparerAndComparison> x, String<TComparerAndComparison> y) => !(x == y);
public static bool operator !=(string x, String<TComparerAndComparison> y) => !(x == y);
public static bool operator !=(String<TComparerAndComparison> x, string y) => !(x == y);
#endregion
#region IndexOf, LastIndexOf, StartsWith, EndsWith
public bool EndsWith(string value) => Value.EndsWith(value, _comparisonType);
public int IndexOf(string value) => Value.IndexOf(value, _comparisonType);
public int IndexOf(string value, int startIndex) => Value.IndexOf(value, startIndex, _comparisonType);
public int IndexOf(string value, int startIndex, int count) => Value.IndexOf(value, startIndex, count, _comparisonType);
public int LastIndexOf(string value) => Value.LastIndexOf(value, _comparisonType);
public int LastIndexOf(string value, int startIndex) => Value.LastIndexOf(value, startIndex, _comparisonType);
public int LastIndexOf(string value, int startIndex, int count) => Value.LastIndexOf(value, startIndex, count, _comparisonType);
public bool StartsWith(string value) => Value.StartsWith(value, _comparisonType);
#endregion
}
}
StringComparerAndComparison.cs
using System;
using StringComparer = System.StringComparer;
using StringComparison = System.StringComparison;
namespace JDanielSmith.System
{
/// <summary>
/// Pass around System.StringComparer and System.StringComparison together.
/// Also, provides a base class for generics.
/// </summary>
public abstract class StringComparerAndComparison
{
internal StringComparer Comparer { get; }
internal StringComparison Comparison { get; }
internal StringComparerAndComparison(StringComparer comparer, StringComparison comparison)
{
if (comparer == null) throw new ArgumentNullException(nameof(comparer));
Comparer = comparer;
Comparison = comparison;
}
}
public sealed class CurrentCulture : StringComparerAndComparison
{
public CurrentCulture() : base(StringComparer.CurrentCulture, StringComparison.CurrentCulture) { }
}
public sealed class CurrentCultureIgnoreCase : StringComparerAndComparison
{
public CurrentCultureIgnoreCase() : base(StringComparer.CurrentCultureIgnoreCase, StringComparison.CurrentCultureIgnoreCase) { }
}
public sealed class InvariantCulture : StringComparerAndComparison
{
public InvariantCulture() : base(StringComparer.InvariantCulture, StringComparison.InvariantCulture) { }
}
public sealed class InvariantCultureIgnoreCase : StringComparerAndComparison
{
public InvariantCultureIgnoreCase() : base(StringComparer.InvariantCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase) { }
}
public sealed class Ordinal : StringComparerAndComparison
{
public Ordinal() : base(StringComparer.Ordinal, StringComparison.Ordinal) { }
}
public sealed class OrdinalIgnoreCase : StringComparerAndComparison
{
public OrdinalIgnoreCase() : base(StringComparer.OrdinalIgnoreCase, StringComparison.OrdinalIgnoreCase) { }
}
}