user158977
user158977

Reputation: 91

C# - Conversion -Clarification

This could be a very basic question for prodigies.But I have a doubt to handle it.

During Conversion we are using :

int.Parse(someThing)
Convert.ToInt32 or Convert.ToString()....

(Int32)(someValue)

even we use "as"

What are the restrictions for using each?

Upvotes: 1

Views: 142

Answers (3)

andreialecu
andreialecu

Reputation: 3729

Convert.ToInt32 can convert from various types (DateTime, Decimal, etc) back to an integer, while int.Parse() only converts from a string.

If you're only parsing strings back to integers, Convert.ToInt32 is just an alias for int.Parse, so you might as well use int.Parse instead.

Regarding casting, you cannot cast a string to an integer, you have to convert it like mentioned above.

Consider that you are working with a database, and you have a column that is inefficiently declared as varchar while it stores integer data. You cannot do (int)dr["column"] when using a SqlDataReader for instance, you would have to use int.Parse(dr["column"].ToString()) instead. If the column was of the int column type in the database, you could then have used the cast: (int)dr["column"].

Upvotes: 0

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

Convert.ToNnnn has the restrictions stipulated by its overloads; for instance, you cannot call Convert.ToMyCustomType (since that method does not exist). You can pass pretty much anything into Convert.ToString (or Convert.ToInt32 or any other of the Convert.ToNnnn methods) but the result may not always make sense. Also, if the method fails to perform the conversion it may throw an InvalidCastException.

The regular type case ((int)someValue) can be used in all cases where there is an explicit conversion available. If you try to perform an illegal cast you will get an exception thrown at you.

The as keyword can be used to cast type to another (reference) type (and it will return null if it's not possible). The as keyword cannot be used with value types (such as Int32, Point or DateTime).

In my own code I typically use a mixture of them, but in general I follow the following scheme:

  • If I want to convert a string to a numeric type, I usually use the TryParse provided by that numeric type.
  • If I want to cast a class instance to a base class or interface, I usually use the as keyword.
  • In other cases I use regular type casting within try/catch blocks.

Upvotes: 1

Wolfwyrd
Wolfwyrd

Reputation: 15916

int.Parse assumes a string as a parameter and as such is only suitable for converting raw string representations to integers

Convert.ToInt32() will attempt to convert pretty much any object to an integer representation. Where the representation isn't a valid int (i.e. using a float with the value 55.3 or a string containing a word) this will raise a FormatException. If the integer is too large to fit in an int then an OverflowException will occur.

(int) is a direct cast. It's basically saying "I know that this object is really an integer, treat it as such". If the object actually isnt an integer you'll get an invalid cast exception.

Finally, as behaves the same as a direct cast except where the object is not of the correct type it will assign null. Not sure how this applies to int with it being a non-nullable type but certainly usable with

int? myInt = someVar as int?;

Upvotes: 4

Related Questions