-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
String to implement IReadOnlyList<char> instead of IEnumerable<char> #4071
Comments
@stas-sultanov Thanks for the suggestion. Can you give an example of where this would be useful? |
Sure. Here is an example from one of my projects: public static TryResult<String> TryToBase16String(byte[] data, int offset, int length)
public static TryResult<Byte[]> TryFromBase16String(string data, int offset, int length) for this methods i'd like to use single method that checks arguments private static void CheckArguments<T>(IReadOnlyCollection<T> data, int offset, int length)
{
if (data == null)
throw new ArgumentNullException(@"data");
if (length < 0)
throw new ArgumentOutOfRangeException(@"length", length, @"Is less than 0.");
if (offset < 0)
throw new ArgumentOutOfRangeException(@"offset", offset, @"Is less than 0.");
if (offset > (data.Count - length))
throw new ArgumentOutOfRangeException(@"data", data.Count, @"Offset plus length is greater than the length of data.");
} But unfortunately it is not possible because byte[] and string has only one common interface |
A non-generic overload of CheckArguments would solve this, of course. |
of course it would, and that it is the way it is made now. :) but it dose not looks like a good design. maybe example is not good enough, however i strongly believe that this small change would made coreclr more versatile. |
Just a quick point but unless I'm wrong array's don't implement |
Arrays do implement IReadOnlyList<int> test = new [] {1, 2, 3}; the Enumerable class that implements Count method is not part of the coreclr. |
Not much perf diff between .Count() and .Length for an array |
Ah yes, I had looked before posting but missed that comment. My bad. So then yes I'm all for making string implement |
indeed, but once again:
|
I believe that this would be nice improvement of the architecture of the .net classes. |
@KrzysztofCwalina How do you think about this? It seems that |
+1 for this, would be really useful to be able to pass strings directly as IReadOnlyList/IReadOnlyCollection parameters instead of going via .ToCharArray() or using a wrapper class. |
To me string is a primitive type. I think it's already a mistake that it implements IEnumerable. |
@KrzysztofCwalina I totally get that view. It leads to special cases in certain code that isn't required in, say, Java. But for better or worse, that ship has sailed. Given that string already implements 💭 I think it makes sense specifically for some cases where code currently needs to call |
If we implemented it explicitly and made sure the Linq extensions don't show on String, then I guess it would be fine. |
@KrzysztofCwalina It already implements |
Hmmm, I was under the impression that we special cased it in C#. |
@jaredpar, @MadsTorgersen, I thought C# was special cased to not show Linq methods on String? |
In the PCL, |
@KrzysztofCwalina I think you just spent too much time with PCL. :-) In the full framework, LINQ always worked over string - for better or worse. |
Yeah, it's all slowly coming to me. I think we initially wanted to special case it, but then we just decided to implement IEnumerable. Then in 8.1 we added IEnumerable (why?), and we are back to the original problem that string intellisense shows all these linq methods that probably should not be used on strings. |
API request -- moving to CoreFX |
Issue moved to dotnet/corefx dotnet/coreclr#14336 via ZenHub |
I would like to suggest to make class String to implement IReadOnlyList{char} instead of IEnumerable{char}.
Possibly interface should be implemented explicitly because of the Length property, which is extern and hardcoded in JIT. On the other hand Length could be deprecated and Count should be used instead.
The text was updated successfully, but these errors were encountered: