What is the difference between using (int)variable or Convert.Toint32(variable)?

Asked

Viewed 1,211 times

16

What is the difference between them? At what times is it more appropriate to use one or the other?

Examples:

string palavra = "10";
var numero = Convert.ToInt32(palavra); // ou (int)palavra ?

string palavra2 = "10.50";
var numero2 = (double)palavra2; // ou Convert.ToDouble(palavra2) ?

Note: the type int I only used as an example, but I wanted to know in general about all types.

  • 3

    Who gave negative comment the reason. If the question is not appropriate I delete or modify.

  • 1

    I did not deny, I think it is better that you put the example of the two in the question as code, in the title when I read had the impression of the (int) as something apart, I think you meant: var num = (int)valor;

  • Thanks @rray, I added some sample code.

2 answers

14


Cast

The (int) is an operator of cast. It should only be used when there is certainty that the conversion will be successful. It can also only convert numeric values. In the example, used the cast for double will not work. It does not foresee this type of conversion.

The cast must be available for the type being used and must be able to handle the type of input. It is possible to create this operator’s implementation for its own types, although in a few cases it makes sense to do so. It is less useful than it seems. And does not perform well in some situations.

Convert

The Convert.ToInt32() is a more complete method that tries to make the conversion wider, may use non-numeric types, such as string. Obviously it is an external utility method to type.

In theory these methods of Convert could make an extra effort to convert objects that are more difficult to convert, try to solve conversion problems more intelligently. In practice this does not usually occur.

In general its use is suitable when you know that the conversion will be successful, ie it is ensuring that it has a valid number in the text or when the source for the conversion is not a string.

Parse

But for string, almost always better wear a int.Parse(), or better yet, a int.TryParse(), after all if you are not sure if the value can be converted it is better to have an error code than an exception.

There are still some implications when using another language with .NET. The cast is part of the type system, the Convert is just an extra utility.

Related: What is the main difference between int. Parse() and Convert.Toint32()?.

Be sure to see: Differences between Parse vs Tryparse.

10

The (int)variável is a cast, that in the case of your question will not work for String (string), giving this error message:

CS0030 Cannot Convert type 'string' to 'double'

In case you can use the Convert, but today we use the int.Parse or better yet int.TryParse.

In fact the Convert is not widely used nowadays due to problems of some conversions ...

Example: ref site and all credits to Convert.Toint32 vs Int32.Parse - Source Code

string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "1234567891234567891123456789123456789";

Convert

result = Convert.ToInt32(s1); //-- 1234
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException

Parse

result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException

Recommending:

Always use methods referring to type conversions int, utilize int.Parse or TryParse and so on, bringing even greater clarity to your code. The Convert has some methods that are used at some point in your code, but, the frequency is low!

  • 1

    Nice examples in code.

  • 2

    The subject is that it is cool saw, congratulations for the questioning, only reinforcing use in almost its entirety the Parse or Tryparse emitted clarity to its code and standardization

  • @bigown my source was removed from the Source Code as link described in the reply.

  • I’m changing the link, because the original is still elsewhere, I hope that the chain of copies/citations has not gone beyond: http://www.codeproject.com/Articles/32885/Difference-Between-Int32-Parse-Convert-ToInt32-and

  • I believe they were already @bigown

Browser other questions tagged

You are not signed in. Login or sign up in order to post.