-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Discussion) Case insensitive strings as a type #15266
Comments
Just thought on one more thing -- #14065 discusses a string comparison operator -- e.g.
If we had case insensitive strings instead, we could have a literal syntax in C#, e.g.
|
Which culture should be used for comparison? If you attach it to the string this leads to the question of what should happen when you compare two case insensitive strings which use different cultures. Or should it always use ordinal comparisons? |
And how would a different type would help? Currently you can write: string[] args = ...;
var set = new HashSet<string>(args, StringComparer.OrdinalIgnoreCase); But if a separate string type is added you'd need something like the following: string[] args = ...;
var set = new HashSet<CaseInsensitiveString>(args.Select(s => (CaseInsensitiveString)s)); It doesn't see how this is better. |
This is a good question. Ordinal seems reasonable, but it's up for debate.
In your example it wouldn't, but let's think on where that string comes from. E.g. let's start with public class ItemType {
public CaseInsensitiveString Name { get; }
} Now some use cases: var typesByName = types.ToDictionary(t => t.Name); var found = types.SingleOrDefault(t => t.Name == nameToFind); var names = types.Select(t => t.Name).ToSet(); // custom, but common extension method |
@mikedn So basically you decide which strings are CI when you are writing your domain model. |
Great for checking keywords. Not so good for anything human-readable. There's enough variation in whether or not |
But in that case a domain specific string type - Of course, there will be cases where And interestingly, one common case where case insensitivity is needed - Windows file names - isn't served well by |
One further step could be instead of a |
That would be good, but the main question is what to do when two of those are compared.
If by "Fail" you mean "return false" -- it would be OK for dynamic cases, but I think it's inconvenient for static comparisons. E.g.
It is possible to do a generic hack using marker types, like |
In perfect world -- yes. However having a .NET type can provide some benefits (most of those can be implemented manually, but many developers will never care enough):
|
Reading through this, it looks like the clear advantage would come if this was actually a language feature. Having a specific type that holds a case insensitive string seems like overkill in most scenarios. @KrzysztofCwalina should we move this issue to the Roslyn repo to see if there is interest from the compiler to implement this? |
Is anybody still driving this? It has been many years since the last comment. |
I have read #14065, and it seems to be a special case of a generic problem.
Consider system where some strings are case sensitive (e.g. hash), and some aren't (e.g. name). Currently .NET defaults to being case sensitive, so I have to remember which string is which in all use cases. And those cases are not only about
s1.Equals(s2)
-- it is also every call toToDictionary
, every creation ofHashSet
and various other cases and optimizations that are really hard to keep track of.It would be great if I could define string as being case-insensitive on type level, e.g. use
CaseInsensitiveString
. This would affect Comparer selection wherever this type is used as a key, and all comparisons.Not quite sure whether it makes general sense and what the API would be like, so just putting it here as a discussion point.
The text was updated successfully, but these errors were encountered: