Do not confuse casting and converting! If the true type of expression is unknown to the compiler, for example. because it is printed as an object , and you know your own type, you can tell the C # compiler: "Hey, I know it a int , so please treat it as an int ."
(int)expression
In your case, the expression is a string expression that cannot be distinguished from int , just beacuase is not int . However, you can convert the string to int if it represents a real integer. In addition, the type of the result of your conversion is unknown, since it is common. Use (T)Convert.ChangeType(...) , as others have already pointed out.
source share