3

I have a question, i'm doing some research on some programming languages. The research is about the efficiency of the substring functions in C# and Java.

Questions like is C# using a brute force kind of way, or do they implement Boyer-Moore's algorithm like a good boy. I need the source code for this, I already found it for Java ( Who use a brute force implementation in the indexOf()method for those who wonder ).

Does anyone have an idea how i can retrieve the source code for methods like these in C#. I have visual studios installed on my laptop but i can't find any source code...

Your help will much obliged!

1 Answer 1

6

Microsoft has published the complete framework source code, including comments. You will find the actual implementation over here on referencesource. For SubString, it comes down to some unmanaged code:

    [System.Security.SecurityCritical]  // auto-generated
    unsafe string InternalSubString(int startIndex, int length) {
        Contract.Assert( startIndex >= 0 && startIndex <= this.Length, "StartIndex is out of range!");
        Contract.Assert( length >= 0 && startIndex <= this.Length - length, "length is out of range!");            

        String result = FastAllocateString(length);

        fixed(char* dest = &result.m_firstChar)
            fixed(char* src = &this.m_firstChar) {
                wstrcpy(dest, src + startIndex, length);
            }

        return result; 

As you can see, they are using wstrcpy which probably is as fast as it gets.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you so much! No idea they released it cause it always was closed source.
@user1758777 Making .NET open source is a very recent development--in the last two months. See blogs.msdn.com/b/dotnet/archive/2014/11/12/….

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.