Tech Off Post

Single Post Permalink

View Thread: Split and Join
  • User profile image

    If I remember correctly (this decision was a loooong time ago), the reason for this was that either .NET 1.0 or a pre-release of .NET had

    string[] String::Split(char ch);
    string[] String::Split(params char[] chs);
    string[] String::Split(IEnumerable<char> chs);

    the third overload was removed from .NET because string is IEnumerable<char> and this led to confusion:

    foreach(var x in "Hello World".Split("el")) Console.WriteLine(x);

    would yield


    Rather than


    as most people would expect.

    Therefore the decision was that the overload string[] String::split(IEnumerable<char> chs) should be removed.

    Unfortunately you can't then add String::Split(string chs), since this means that you've just changed what "Foo".Split("Bar") means (it used to mean split by 'B', 'a' and 'r', since string is a collection of chars and matches IEnumerable<char> and now it means split by "Bar" since you have an overload of string).

    So long story short is that a lot of this nastiness is there for frankly pretty old reasons. Adding overloads in future probably isn't a bad idea, but since most people have been coping (string.Split(x, new string[] { y }) does what most people expect), I think this has been pretty low down the list of priorities for .NET. You need a good reason to change the base library once it's used by millions of customers, and I'm not sure this is a good enough reason to change it.