Which is fast comparison: Convert.ToInt32(stringValue)==intValue or stringValue==intValue.ToString()

后端 未结 8 2067
执念已碎
执念已碎 2021-02-13 18:34

While developing my application i came across some comparison stuff here was it:

    string str = \"12345\";
    int j = 12345;
    if (str == j.ToString())
             


        
8条回答
  •  醉酒成梦
    2021-02-13 19:01

    Well for starters the first one which converts the int to a string won't throw an error if the string the int is being compared to isn't convertible to an int.

    If you are doing a lot of tests in a batch converting to a string will remove the problem of potentially having exceptions throw, because of conversion errors. Raising exceptions takes time and would slow down the second test.

提交回复
热议问题