1

One of the questions that i always faced was the implementation of .NET Framework class libraries.

I know some of the methods original implementation:

For example :

MessageBox.Show("...");

As i know this method must have used P/Invoke to call Win32 API.

but something like this:

System.Convert.ToInt32(mystr);

I actually don't know how it works because conversion between int and string is not possible in pure C#.(Can you do exact same thing without using that method? Actually I don't know).

Finally if you know the answer please clarify these concepts for me speicially the 2nd example.

Amirreza H
  • 63
  • 1
  • 9
  • 3
    Source code of a big part of the NET.Framework is [available here](http://referencesource.microsoft.com/) – Steve Dec 30 '14 at 20:25
  • @Steve Thank you but i could see the source myself using reflector.But i don't understand it! – Amirreza H Dec 30 '14 at 20:26
  • 1
    of course you can write a function to parse a string and return an int in pure C# – Jason Dec 30 '14 at 20:26
  • "Can you do exact same thing without using that method? Actually Nope)" - `int.Parse()`? – Rhumborl Dec 30 '14 at 20:26
  • @Rhumborl But this is an alternative to that method. If i remove the power of .net framework from c#,can it do this purely? – Amirreza H Dec 30 '14 at 20:29

4 Answers4

6

Can you do exact same thing without using that method? Actually Nope.

You absolutely can. Here's a really inefficient way of doing it - which doesn't consider overflow, invalid input or negative numbers, but demonstrates the general principle.

int ParseStringToInt32(string text)
{
    int result = 0;
    foreach (char c in text)
    {
        result = result * 10 + (c - '0');
    }
    return result;
}

Fundamentally there's nothing mystical about the process of parsing a string as an Int32. It's just a case of looking at each character, considering its numeric value, and doing some arithmetic.

Indeed, there are times when it's worth doing it manually - in Noda Time we have our own numeric parsing code to allow a limited number of characters to be parsed without having to take a substring.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Wow thank you.You turned on the lamp in my brain. What about communication directly with CLR? how it is possible? – Amirreza H Dec 30 '14 at 20:31
  • @AmirrezaH: What do you mean by "communication directly with CLR"? Certain methods have a native implementationa which is CLR-specific, sure... – Jon Skeet Dec 30 '14 at 20:32
  • I mean that using specified CLR APIs. Low level types management. I'm sure that you have seen them somewhere. – Amirreza H Dec 30 '14 at 20:34
  • something that bothers is the understanding the relation of .NET framework and C# because .NET framework does not act only as a API.It controls types,exceptions,memory management,etc. – Amirreza H Dec 30 '14 at 20:35
  • @AmirrezaH: If you mean things like `GC.Collect`, that's likely to be a CLR-specific implementation. – Jon Skeet Dec 30 '14 at 20:41
  • .NET is not specific to C#. Same .NET is used by VB.NET, and you can use .NET Framework classes from PowerShell. – John Saunders Dec 31 '14 at 02:24
  • @John: All true, but I'm not sure how it relates to the question, which is about how the framework is implemented - and that *is* mostly on C# as far as I'm aware. – Jon Skeet Dec 31 '14 at 08:02
  • Jon, I was referring to the comment "something that bothers is the understanding the relation of .NET framework and C#" – John Saunders Dec 31 '14 at 12:58
5

Microsoft has made the BCL available online at: http://referencesource.microsoft.com

Calling Convert.ToInt32(string) will eventually call int.Parse, which in turn will eventually call the actual routine on an internal Number class here:

One of the basic routines listed there is as follows:

    [System.Security.SecuritySafeCritical]  // auto-generated
    private unsafe static Boolean NumberToInt32(ref NumberBuffer number, ref Int32 value) {

        Int32 i = number.scale;
        if (i > Int32Precision || i < number.precision) {
            return false;
        }
        char * p = number.digits;
        Contract.Assert(p != null, "");
        Int32 n = 0;
        while (--i >= 0) {
            if ((UInt32)n > (0x7FFFFFFF / 10)) {
                return false;
            }
            n *= 10;
            if (*p != '\0') {
                n += (Int32)(*p++ - '0');
            }
        }
        if (number.sign) {
            n = -n;
            if (n > 0) {
                return false;
            }
        }
        else {
            if (n < 0) {
                return false;
            }
        }
        value = n;
        return true;
    }
Ani
  • 111,048
  • 26
  • 262
  • 307
1

try this: it's not the Microsoft implementation, since it's not open source. but it should give you an idea

https://github.com/mono/mono/blob/master/mcs/class/Managed.Windows.Forms/System.Windows.Forms/MessageBox.cs

ro-E
  • 279
  • 1
  • 5
  • 16
1

Implementation of MessageBox.Show is here.

Implementation of Convert.ToString is here.

Frank
  • 4,461
  • 13
  • 28