What is fastest: (int), Convert.ToInt32(x) or Int32.Parse(x)?

后端 未结 15 1171
南笙
南笙 2021-02-02 06:13

Which of the following code is fastest/best practice for converting some object x?

int myInt = (int)x;

or

int myInt = Convert.T         


        
15条回答
  •  一整个雨季
    2021-02-02 06:53

    Not sure about performance, but these methods aren't the same at all. Both Parse and TryParse work with string, the String representation of an object is parsed (see MSDN).

    Converts the string representation of a number to its 32-bit signed integer equivalent.

    Not sure about casting and the Convert class, but cast is only for objects that are already integers in fact but not strongly typed.

    Matthias

提交回复
热议问题