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

前端 未结 13 2402
-上瘾入骨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: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.

提交回复
热议问题