views:

115

answers:

5

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)
+11  A: 

(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.

Dean Harding
@ codeka Thank a lot
4thpage
(int) will work on more than just an object that is an int. If you have a double for instance, you can cast it to an int using (int) such as:double value1 = 5.5;int value2 = (int)value1;value2 will be equal to 5.The difference you pointed out is that (int) will not attempt to parse a string value to an int.
ManiacZX
+3  A: 

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.

this. __curious_geek
+3  A: 

There are two scenarios where (int) something works:

  1. 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...

  2. 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)

SWeko
+4  A: 

In the general sense, (Type)val can represent:

  • an inbuilt conversion (for example, between a float an an int), defined by the language
    • which might require an actual operation (float <===> int being a good example)
    • or might just be for the beefit of the compiler (int <===> an-int-enum, for example)
  • a reference cast (which never changes the reference or the object; it just types the local handle to it)
    • which might require a type check (for example, object ===> IDataReader)
    • but might be already known to be true (for example, IDataReader ===> object)
  • a boxing / unboxing operation (which always initializes a value-type, or creates a new object on the heap [except for null-boxing a Nullable<T>]
  • a type-defined conversion operator (implicit or explicit), which runs whatever code is defined in the type

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).

Marc Gravell
+1  A: 

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");
  }
}
ManiacZX
thanks a lot that would be great.
4thpage