You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
StringSplitOptions is missing an option to remove duplicate strings from the resulting array leaving each string their own unique item when the user does not want duplicate entries of the same string within the resulting array from calls to string.Split(...).
A valid usage of this is when you are making a source generator and you want to filter out the obtained string from an AdditionalFile that the C# compiler loaded up for you by each line like so:
But then you also want Split(...) to not include duplicate lines from said AdditionalFile if they contain such but yet Split(...) does not do such a thing (yet) making you have to try to probe for duplicate entries in the array then remove and resize said array after the fact of it's creation.
After that is done then you can process those lines to generate code for the user and then everything would compile fine on their end.
Risks
I think the risk for this is minimal and the functionality should be trivial to add (hopefully).
Why not simply pipe the resulting array into a HashSet<string>? That way you can also pass in the kind of comparison you want to do (which is heavily recommended to do any time string operations are involved).
Reacted by Petr Onderka, Zhenwei Wu, pinkfloydx33, Fraser Ashyggton and Miha Zupan
@AraHaan If it is, then use Distinct(). The point is that there is no reason to embed this functionality directly into string.Split, when the BCL already lets you easily and efficiently do it today.
Another benefit to piping this to a HashSet<string> is that you can specify which comparer you want to use. For example, you can specify whether you want duplicate checking done in a case-sensitive or a case-insensitive fashion.
I agree with the feedback: if the consumer needs duplicates removed, they should remove the duplicates using HashSet<T> or Distinct() (which just uses HashSet<T> today). Thanks for the suggestion.
Activity
WeihanLi commentedon Aug 3, 2021
RemoveDuplicates = 4is more reasonable for a flagged enumAraHaan commentedon Aug 3, 2021
Edited, thanks.
Joe4evr commentedon Aug 3, 2021
Why not simply pipe the resulting array into a
HashSet<string>? That way you can also pass in the kind of comparison you want to do (which is heavily recommended to do any time string operations are involved).AraHaan commentedon Aug 3, 2021
But isn't
HashSet<string>slower than usingDistinct()fromSystem.Linq;?svick commentedon Aug 3, 2021
@AraHaan If it is, then use
Distinct(). The point is that there is no reason to embed this functionality directly intostring.Split, when the BCL already lets you easily and efficiently do it today.GrabYourPitchforks commentedon Aug 3, 2021
Another benefit to piping this to a
HashSet<string>is that you can specify which comparer you want to use. For example, you can specify whether you want duplicate checking done in a case-sensitive or a case-insensitive fashion.stephentoub commentedon Mar 14, 2022
I agree with the feedback: if the consumer needs duplicates removed, they should remove the duplicates using
HashSet<T>orDistinct()(which just usesHashSet<T>today). Thanks for the suggestion.