示例#1
0
        /// <summary>
        /// Tries to convert the string representation of a number to its <see cref="Byte"/> equivalent, and returns a value that indicates whether the conversion succeeded.
        /// </summary>
        /// <param name="s">A <see cref="string"/> that contains a number to convert.</param>
        /// <param name="result">When this method returns, contains the <see cref="byte"/> value equivalent to the number contained in <paramref name="s"/> if the conversion succeeded, or zero if the conversion failed. This parameter is passed uninitialized; any value originally supplied in <paramref name="result"/> will be overwritten.</param>
        /// <returns>true if <paramref name="s"/> was converted successfully; otherwise, <see langword="false"/>.</returns>
        /// <remarks>
        /// The conversion fails and the method returns <see langword="false"/> if the s parameter is not in the correct format, if it is <see langword="null"/> or <see cref="string.Empty"/>, or if it represents a number less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/>.
        /// The <see cref="TryParse"/>(String, Byte) method is similar to the Byte.Parse(String) method, except that <see cref="TryParse"/>(String, Byte) does not throw an exception if the conversion fails.
        /// </remarks>
        public static bool TryParse(string s, out byte result)
        {
            result = (byte)Convert.NativeToInt64(
                s,
                true,
                MinValue,
                MaxValue,
                10,
                false,
                out bool success);

            return(success);
        }
示例#2
0
        /// <summary>
        /// Converts the string representation of a number to its 64-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded.
        /// </summary>
        /// <param name="s">Converts the string representation of a number to its 64-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded.</param>
        /// <param name="result">When this method returns, contains the 64-bit unsigned integer value equivalent of the number contained in <paramref name="s"/>, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the <paramref name="s"/> parameter is <see langword="null"/> or <see cref="string.Empty"/>, is not of the correct format, or represents a number less than <see cref="MinValue"/> or greater than <see cref="MaxValue"/>. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
        /// <returns><see langword="true"/> if s was converted successfully; otherwise, <see langword="false"/>.</returns>
        /// <remarks>
        /// The <see cref="TryParse"/> method is like the <see cref="Parse"/> method, except the <see cref="TryParse"/> method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a <see cref="FormatException"/> in the event that <paramref name="s"/> is invalid and cannot be successfully parsed.
        /// </remarks>
        public static bool TryParse(
            string s,
            out ulong result)
        {
            // the interface use long for min/max, and uint64 is bigger. Setting min/max to 0/0 will cause the native code to calculate the largest value and return it as Int64 which when cast to UInt64 returns the larger numbers that a UInt64 can reach
            result = (ulong)Convert.NativeToInt64(
                s,
                true,
                0,
                0,
                10,
                false,
                out bool success);

            return(success);
        }