2

That is if we were to see how Microsoft wrote this method what would it look like? I'm mainly interested in the use of the StringSplitOptions enumeration with the other parameter and how they probably structured their code to account for each option.

1 Answer 1

8

To find out what the implementation looks like for any part of the Framework, you can do one of two things:

  1. You can download the .NET Framework Source Code from here, or
  2. You can use a decompiler, like Telerik JustDecompile.

The latter choice is by far the easiest.

I downloaded and installed JustDecompile, and loaded up .NET Framework 3.5. Then I opened up mscorlib.dll (where the String class is located) and looked at the Split methods.

Without getting into too much detail, there's an if statement that checks the StringSplitOptions enumeration, and runs one of two different methods depending on which StringSplitOptions setting is specified. The two methods are called InternalSplitKeepEmptyEntries and InternalSplitOmitEmptyEntries. They are pretty much two completely different implementations.

4
  • 1
    The source to the method is now online: referencesource.microsoft.com/#mscorlib/system/string.cs
    – JacquesB
    Commented Jul 10, 2016 at 16:10
  • -1 DeCompiling shows the compiler's ultimate interpretation of the developer's implementation, not the actual implementation. It's very possible you'll see an optimized version of the code.
    – RubberDuck
    Commented Jul 11, 2016 at 0:57
  • @RubberDuck: Which is why I also mentioned the Reference Source. That said, the decompilation and the original source don't differ all that much; many of the transformations you would see amount to syntactic sugar, and much of the optimization occurs in the JIT, not in the compiler. Which you would have known had you compared the reference source to JustDecompile's output. Commented Jul 11, 2016 at 1:10
  • In this case, maybe, but the compiler can do a lot of arcane magic, particularly when async/await or lambda expressions come into play.
    – RubberDuck
    Commented Jul 11, 2016 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.