What's the main difference between int.Parse() and Convert.ToInt32

前端 未结 13 2173
-上瘾入骨i
-上瘾入骨i 2020-11-22 08:47
  • What is the main difference between int.Parse() and Convert.ToInt32()?
  • Which one is to be preferred
相关标签:
13条回答
  • 2020-11-22 08:50
    Convert.ToInt32
    

    has 19 overloads or 19 different ways that you can call it. Maybe more in 2010 versions.

    It will attempt to convert from the following TYPES;

    Object, Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, String, Date

    and it also has a number of other methods; one to do with a number base and 2 methods involve a System.IFormatProvider

    Parse on the other hand only has 4 overloads or 4 different ways you can call the method.

    Integer.Parse( s As String)
    
    Integer.Parse( s As String,  style As System.Globalization.NumberStyles )
    
    Integer.Parse( s As String, provider As System.IFormatProvider )
    
    Integer.Parse( s As String,  style As System.Globalization.NumberStyles, provider As System.IFormatProvider )
    
    0 讨论(0)
  • 2020-11-22 08:52

    Parse() methods provide the number styles which cannot be used for Convert(). For example:

    int i;
    bool b = int.TryParse( "123-",
               System.Globalization.NumberStyles.AllowTrailingSign,
               System.Globalization.CultureInfo.InvariantCulture,
               out i);
    

    would parse the numbers with trailing sign so that i == -123
    The trailing sign is popular in ERP systems.

    0 讨论(0)
  • 2020-11-22 08:53

    for clarification open console application, just copy below code and paste it in static void Main(string[] args) method, I hope you can understand

    public  class Program
        {
            static void Main(string[] args)
            { 
                int result;
                bool status;
                string s1 = "12345";
                Console.WriteLine("input1:12345");
                string s2 = "1234.45";
                Console.WriteLine("input2:1234.45");
                string s3 = null;
                Console.WriteLine("input3:null");
                string s4 = "1234567899012345677890123456789012345667890";
                Console.WriteLine("input4:1234567899012345677890123456789012345667890");
                string s5 = string.Empty;
                Console.WriteLine("input5:String.Empty");
                Console.WriteLine();
                Console.WriteLine("--------Int.Parse Methods Outputs-------------");
                try
                {
                   result = int.Parse(s1);
    
                   Console.WriteLine("OutPut1:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut1:"+ee.Message);
                }
                try
                {
                  result = int.Parse(s2);
    
                  Console.WriteLine("OutPut2:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut2:" + ee.Message);
                }
                try
                {
                   result = int.Parse(s3);
    
                   Console.WriteLine("OutPut3:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut3:" + ee.Message);
                }
                try
                {
                    result = int.Parse(s4);
    
                    Console.WriteLine("OutPut4:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut4:" + ee.Message);
                }
    
                try
                {
                     result = int.Parse(s5);
    
                     Console.WriteLine("OutPut5:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut5:" + ee.Message);
                }
                Console.WriteLine();
                Console.WriteLine("--------Convert.To.Int32 Method Outputs-------------");
                try
                {
    
                    result=  Convert.ToInt32(s1);
    
                    Console.WriteLine("OutPut1:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut1:" + ee.Message);
                }
                try
                {
    
                    result = Convert.ToInt32(s2);
    
                    Console.WriteLine("OutPut2:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut2:" + ee.Message);
                }
                try
                {
    
             result = Convert.ToInt32(s3);
    
             Console.WriteLine("OutPut3:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut3:" + ee.Message);
                }
                try
                {
    
                      result = Convert.ToInt32(s4);
    
                      Console.WriteLine("OutPut4:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut4:" + ee.Message);
                }
    
                try
                {
    
                     result = Convert.ToInt32(s5);
    
                     Console.WriteLine("OutPut5:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut5:" + ee.Message);
                }
    
                Console.WriteLine();
                Console.WriteLine("--------TryParse Methods Outputs-------------");
                try
                {
    
                    status = int.TryParse(s1, out result);
                    Console.WriteLine("OutPut1:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut1:" + ee.Message);
                }
                try
                {
    
                    status = int.TryParse(s2, out result);
                    Console.WriteLine("OutPut2:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut2:" + ee.Message);
                }
                try
                {
    
                    status = int.TryParse(s3, out result);
                    Console.WriteLine("OutPut3:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut3:" + ee.Message);
                }
                try
                {
    
                    status = int.TryParse(s4, out result);
                    Console.WriteLine("OutPut4:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut4:" + ee.Message);
                }
    
                try
                {
    
                    status = int.TryParse(s5, out result);
                    Console.WriteLine("OutPut5:" + result);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("OutPut5:" + ee.Message);
                }
    
    
                Console.Read();
            }
        }
    
    0 讨论(0)
  • 2020-11-22 08:54

    Here is a detail for int.Parse and Convert.ToInt32: Say, you have a char array, char[] a=['1','2','3','4'] and want to convert each element into an integer. The Convert.ToInt32(a[0]) will give you a number of 49. It treats it as ASCII code The int.Parse(a[0]) will give you the right output which is 1

    If you have a string array string[] b=['1','2','3','4'], then Convert.ToInt32 and int.Parse will have no difference in output. Both return the right integer.

    0 讨论(0)
  • 2020-11-22 08:55
    • If you've got a string, and you expect it to always be an integer (say, if some web service is handing you an integer in string format), you'd use Int32.Parse().

    • If you're collecting input from a user, you'd generally use Int32.TryParse(), since it allows you more fine-grained control over the situation when the user enters invalid input.

    • Convert.ToInt32() takes an object as its argument. (See Chris S's answer for how it works)

      Convert.ToInt32() also does not throw ArgumentNullException when its argument is null the way Int32.Parse() does. That also means that Convert.ToInt32() is probably a wee bit slower than Int32.Parse(), though in practice, unless you're doing a very large number of iterations in a loop, you'll never notice it.

    0 讨论(0)
  • 2020-11-22 08:56

    It depends on the parameter type. For example, I just discovered today that it will convert a char directly to int using its ASCII value. Not exactly the functionality I intended...

    YOU HAVE BEEN WARNED!

    public static int ToInt32(char value)
    {
        return (int)value;
    } 
    
    Convert.ToInt32('1'); // Returns 49
    int.Parse('1'); // Returns 1
    
    0 讨论(0)
提交回复
热议问题