23

In VB.NET, is there any difference between Integer and Int32?

If yes, please explain.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vikram
  • 3,996
  • 8
  • 37
  • 58
  • @MitchWheat...Google gave me confusing answers for this. So, thought of asking over here. I know how to search. – Vikram Mar 08 '13 at 06:17
  • 2
    OK, Int32 and Integer are different. Which would you recommend using in projects where there is no pre-existing standard - and why? Presumably this thought was behind @Vikram's question. – finch May 26 '14 at 06:10

1 Answers1

35

Functionally, there is no difference between the types Integer and System.Int32. In VB.NET Integer is just an alias for the System.Int32 type.

The identifiers Int32 and Integer are not completely equal though. Integer is always an alias for System.Int32 and is understood by the compiler. Int32 though is not special cased in the compiler and goes through normal name resolution like any other type. So it's possible for Int32 to bind to a different type in certain cases. This is very rare though; no one should be defining their own Int32 type.

Here is a concrete repro which demonstrates the difference.

Class Int32

End Class

Module Module1
    Sub Main()
        Dim local1 As Integer = Nothing
        Dim local2 As Int32 = Nothing
        local1 = local2 ' Error!!! 
    End Sub
End Module

In this case local1 and local2 are actually different types, because Int32 binds to the user defined type over System.Int32.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    @JaredPar, Thanks for explanation and thanks a lot for not putting some comment like "your google broken?" or something. :) – Vikram Mar 08 '13 at 06:11
  • 2
    @MitchWheat i googled the question and didn't find a suitable explanation. Most links just say "no difference" which is incorrect. There is one answer on the msdn website which had a decent explanation but it was fairly far down in my google results – JaredPar Mar 08 '13 at 06:13
  • 1
    There is no difference. Your answer states "Fuctionally there is no difference between the types" – Mitch Wheat Mar 08 '13 at 06:14
  • I agree with JaredPar. That was the same reason I asked the question over here. I always believe that SO is the platform I can trust...of course, one has to face some rude comments! But, I ignore for gaining knowledge. – Vikram Mar 08 '13 at 06:15
  • @MitchWheat there is a difference in how the compiler interprets the names. This is an often overlooked difference and can bite people (especially in generated code). You see the same effect in C# with `int` and `Int32` – JaredPar Mar 08 '13 at 06:16
  • When was the last time you saw Int32 not equal to System.Int32? – Mitch Wheat Mar 08 '13 at 06:18
  • 1
    @MitchWheat I used to work on the .Net compiler team. I've seen this bug pop up several times (in both C# and VB.Net). As I said, people run into this in generated code or in certain nested type situations. It's not a fictional issue – JaredPar Mar 08 '13 at 06:20
  • That's cool. Glad I stuck around to see this answer. I'm surprised you can do that without brackets i.e. `Class [Int32]`. Thanks – djv Mar 08 '13 at 06:24
  • @DanVerdolino that's very much related. `Int32` isn't a keyword in Vb.Net although many believe it to be. It's just another name that's interpreted like anything else – JaredPar Mar 08 '13 at 06:25
  • @Jared: you might want to have a word with one of your colleagues! : http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/2912adc7-6d11-4d2d-aa8b-60ddf7c3a3e5 – Mitch Wheat Mar 08 '13 at 06:26
  • 2
    @Mitch that's the post I originally saw when I Googled. SO prevails. – djv Mar 08 '13 at 06:27
  • @MitchWheat yes that answer is incorrect. Those forums are apparently closed and I cannot offer another answer. I will sent Matt an email. – JaredPar Mar 08 '13 at 06:30
  • Bit of a contrived example. If people are defining there own Int32 they reap what they sow. – Mitch Wheat Mar 08 '13 at 06:30
  • 1
    @MitchWheat it's a contrived example to concretely demonstrate the problem. As I said, the most common way this comes up is in generated code. – JaredPar Mar 08 '13 at 06:31
  • So if the question was "Is there a difference between `Integer` and `System.Int32`, then the answer would be no. Generated code *should* specify the namespace to prevent this problem from ever happening. – mbomb007 Mar 23 '15 at 15:06