DWQA QuestionsCategory: ProgramAbout. Net source code Convert.ToString (int16, int32).
Charles asked 2 months ago

It’s used today Convert.ToString (int16, int32) conversion system, very curious about how the. Net framework is implemented, I checked the. Net source code. The version of. Net source code I used is dotnet_ v4.6.2_ RS1 in NDP Convert.ToString (int16, int32) method is defined as follows:

        // Convert the byte value to a string in base fromBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (byte value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI1);
        }

        // Convert the Int16 value to a string in base fromBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (short value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.IntToString((int)value, toBase, -1, ' ', ParseNumbers.PrintAsI2);
        }

        // Convert the Int32 value to a string in base toBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (int value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.IntToString(value, toBase, -1, ' ', 0);
        }

        // Convert the Int64 value to a string in base toBase
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static String ToString (long value, int toBase) {
            if (toBase!=2 && toBase!=8 && toBase!=10 && toBase!=16) {
                throw new ArgumentException(Environment.GetResourceString("Arg_InvalidBase"));
            }
            Contract.EndContractBlock();
            return ParseNumbers.LongToString(value, toBase, -1, ' ', 0);
        }

Parsenumbers is an internal class. Under this class, the implementation of inttostring is as follows:

        [System.Security.SecurityCritical]  // auto-generated
        [ResourceExposure(ResourceScope.None)]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        public extern static String IntToString(int l, int radix, int width, char paddingChar, int flags);

This method uses the extern keyword. It should have called the external implementation, but the specific call is not found. Please ask the gods for guidance.

2 Answers
RickyLin answered 2 months ago

MethodImplAttribute(MethodImplOptions.InternalCall)It shows that the implementation is in the CLR. Before the. Net core, Microsoft’s CLR seems to be closed source, so basically no implementation code can be seen. But since. Net core, Microsoft has been embracing open source in an all-round way. Its so-called coreclr is no exception. I found it in the 1.0.0 source code of coreclrIntToStringC of++realizationAt present, the latest. Net core 2.0 + (coreclr 2.0 +) has been changed to C#realization.

A confused walking man answered 2 months ago

See if there is no definition in the feature