Reputation:
when i convert a object to int by
(int)object
then when the object value is 0 then he give me error that specific cast not valid.
when i convert a object to int by
convert.toint32(object)
then he works and give me 0 means cast is valid.
and i want to know that what is difference between both.
1. (int)object
2.convert.toint32(object)
Upvotes: 4
Views: 2677
Reputation: 867
There are many ways to convert to an int, a lot depends on what your source is.
The biggest thing to keep in mind is error checking, none of the methods are fool proof on their own and so you need to decide how you want to approach them.
Casting with (int), Converting with Convert.ToInt32(), Parsing with int.Parse() all can generate exceptions such as InvalidCastException, FormatException and OverflowException and should use try/catch to handle failed result.
Parsing with int.TryParse() will return a true/false result of if the parsing was successful and if successful set the value to the out parameter given in the function call.
If you are truly trying to take any object and turn it into an int, you are probably best with Convert.ToInt32 such as:
public void TestFunction(object input)
try {
int value = Convert.ToInt32(input);
SomeOtherFunction(value);
}
catch (Exception ex) {
Console.WriteLine("Could not determine integer value");
}
}
Another possibility would be relying on the object producing a usable value in .ToString() such as:
public void TestFunction(object input)
try {
int value = int.Parse(input.ToString());
SomeOtherFunction(value);
}
catch (Exception ex) {
Console.WriteLine("Could not determine integer value");
}
}
Upvotes: 2
Reputation: 1064114
In the general sense, (Type)val
can represent:
Nullable<T>
]Given an object
value, the (int)object
will assume it is an unboxing operation, so will only work if the object is a boxed int
(or a boxed int-enum). It will fail if the object value is actually a string or a boxed-float
etc.
Convert.ToInt32
works differently; it runs a number of tests, to attempt to bring together things like casting-from-a-float (covered above) and parsing a string (which maps to int.Parse
).
Upvotes: 3
Reputation: 30912
There are two scenarios where (int) something
works:
The something
is an int that was boxed into an object, and we use the call to unbox it. This is what happens if you put the int into an ArrayList, or in HttpSession, etc...
The something
is not an int, but it's type can be explicitly converted into an int, for example, short, long, float of the build-in types, or a type that implemented the explicit cast operator.
Convert.ToInt32
, on the other hand, simply calls something
's type IConvertible.ToInt32 method, and it's more or less equivalent to int.Parse(something)
Upvotes: 2
Reputation: 43217
Both methods are completely different. First one is casting
and the second one is conversion
.
Conversion
is used when a non-integer
value requires to be converted into int.
Casting
is used to unbox
the instance back to int
when the instance is already a type of int
boxed into Object
type.
Upvotes: 4
Reputation: 72678
(int)
works only if the object actually is an integer. For example, (int) "12"
will give you an invalid cast exception.
Convert.ToInt32
tries it's best to convert whatever you give it into an integer. So Convert.ToInt32("12")
will return 12. To be precise, if the object implements IConvertible (which System.String does), then Convert.ToInt32
will call the IConvertible.ToInt32 method.
Upvotes: 13