1

In a code example:

int age;
...
age = (int)Convert.ToInt32(Console.ReadLine());

I see no point to put (int) before conversion. Can I safely remove it to:

age = Convert.ToInt32(Console.ReadLine());

There are the same cases, such as:

height = (double)Convert.ToDouble(Console.ReadLine());
zahmati
  • 1,261
  • 1
  • 10
  • 18

1 Answers1

0

The cast here is a no-op, as ToInt32 returns an int anyway and int and Int32 are the same thing.

So yes, you can and should remove the cast there. My guess is that the original author of the code just did not know C# very well.

If you're converting an Int64 that's really a long so you should assign it to a long and not an integer. If all you care is being able to use it as a number and you're not returning it - I recommend just assigning it to a var and letting type inference take care of it for you.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504