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

后端 未结 8 2052
执念已碎
执念已碎 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:18

    Semantics are a little different. "01" == 1.ToString() is false, 1 == Convert.ToInt32("01") is true.

    If the parsing can go wrong (The string isn't a valid number) then Int32.TryParse is faster than to use Convert.ToInt32().

提交回复
热议问题