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

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

    Well - performance should not be the only thing that matters.

    You should ask whether you want to compare the actual value or only the representation of the number.

    Take the following example: Does "00001" equal 1? If you want it to convert the string to int using Int.TryParse in combination and then compare them.

    There might be other differences, too depending of the local settings. Maybe the user has set to format numbers like "1,000,000" - if you'd compare that string to 1000000.ToString() the result would be false.

提交回复
热议问题