3

Possible Duplicate:
C#, int or Int32? Should I care?

I'm using GetPosition(this) when MouseMoved event gets triggered so having:

Point pt = e.GetPosition(this);

As far as I see both following type casts work but which one is recommended and why?

int x = (int)pt.X;
int x = (Int32)pt.X;
Community
  • 1
  • 1
amit kohan
  • 1,612
  • 2
  • 25
  • 47
  • samething, when its converted to IL int will be int32 so don't worry @amit kohan – S3ddi9 Aug 07 '12 at 00:41
  • recommended that you use the data type provided by the language. So for C# it would be `int`. – AMissico Aug 07 '12 at 00:45
  • My rule of thumb is to go with the data type specified in the documentation. If the `X` and `Y` properties of `Point` are documented as `int`s, then that is what I would use. – HABO Aug 07 '12 at 00:45
  • @HansPassant The `X` property I'm pretty sure is a double. – Chris Sinclair Aug 07 '12 at 01:20

1 Answers1

9

They do exactly the same thing - they'll even compile to the same IL. (Assuming you haven't got some crazy other Int32 type somewhere...)

int is just an alias for global::System.Int32.

(This doesn't just apply to casting - it's almost anywhere that a type name is used.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • They have different semantics, though, since `Int32` is an ordinary identifier which could stand for any value (like variable or property access) or type (you could have types in other namespaces also called `Int32`). On the other hand `int` is always a (particular) type. One consequence is if you cast the negation (unary minus) of something, it is OK to say **`(int)-pt.X`**. However **`(Int32)-pt.X`** will always be seen as a subtraction (binary minus), even if `Int32` could only be a type with the given names in the given scope. Of course you can fix that with `(Int32)(-pt.X)`. – Jeppe Stig Nielsen Apr 22 '15 at 14:29