Type conversion
-
Hello everyone: What is the difference b/w
int x = (int)MyValue;
andint x = System.Convert.ToInt32(MyValue);
which should be used when and why? Thanks. We are using an automated tool for standard validation and its fussing about (int) way of type conversion, can someone plz explain the difference b/w the two. Thanks. Robert -
Hello everyone: What is the difference b/w
int x = (int)MyValue;
andint x = System.Convert.ToInt32(MyValue);
which should be used when and why? Thanks. We are using an automated tool for standard validation and its fussing about (int) way of type conversion, can someone plz explain the difference b/w the two. Thanks. RobertHi Robert Both ways do similar things, but it depends what the actual Type of MyValue is. (int)MyValue is casting MyValue to type int. This means that MyValue has to be an int in the first place, or a type inherited from int (which isn't possible of course!). This is a faster method, because very little has to be checked or changed in memory, but you have to know that MyValue is actually an int. Convert.ToInt32(MyValue) allows you to create an int from other types, e.g. a string like "56". This is useful if you aren't sure what type MyValue is. Because it has to work out what to do with the value tho, this method can be a lot slower than casting directly. For more info, see MSDN: Casting in C#: http://msdn2.microsoft.com/en-us/library/ms173105(VS.80).aspx Convert Class: http://msdn2.microsoft.com/en-us/library/system.convert.aspx Hope this helps Phil
-
Hi Robert Both ways do similar things, but it depends what the actual Type of MyValue is. (int)MyValue is casting MyValue to type int. This means that MyValue has to be an int in the first place, or a type inherited from int (which isn't possible of course!). This is a faster method, because very little has to be checked or changed in memory, but you have to know that MyValue is actually an int. Convert.ToInt32(MyValue) allows you to create an int from other types, e.g. a string like "56". This is useful if you aren't sure what type MyValue is. Because it has to work out what to do with the value tho, this method can be a lot slower than casting directly. For more info, see MSDN: Casting in C#: http://msdn2.microsoft.com/en-us/library/ms173105(VS.80).aspx Convert Class: http://msdn2.microsoft.com/en-us/library/system.convert.aspx Hope this helps Phil
philip_cole wrote:
(int)MyValue is casting MyValue to type int. This means that MyValue has to be an int in the first place, or a type inherited from int (which isn't possible of course!). This is a faster method, because very little has to be checked or changed in memory, but you have to know that MyValue is actually an int.
Some remarks and I would appriciate your input.
(int)Something
conversion is applicable when Something is a numeric type. It just does the convertion without caring what data is lost. I did sometesting and here is what i came acrossstring a1="5"; int b1=(int)a1;
result in a compiler errorobject a1="5"; int b1=(int)a1;
result in runtime casting exception Error.