VB.NET vs C# integer division

12,458

Solution 1

When you look at the IL-code that those two snippets produce, you'll realize that VB.NET first converts the integer values to doubles, applies the division and then rounds the result before it's converted back to int32 and stored in y.

C# does none of that.

VB.NET IL Code:

IL_0000:  ldc.i4.s    10 
IL_0002:  stloc.1     
IL_0003:  ldc.i4.s    0A 
IL_0005:  stloc.0     
IL_0006:  ldloc.1     
IL_0007:  conv.r8     
IL_0008:  ldloc.0     
IL_0009:  conv.r8     
IL_000A:  div         
IL_000B:  call        System.Math.Round
IL_0010:  conv.ovf.i4 
IL_0011:  stloc.2     
IL_0012:  ldloc.2     
IL_0013:  call        System.Console.WriteLine

C# IL Code:

IL_0000:  ldc.i4.s    10 
IL_0002:  stloc.0     
IL_0003:  ldc.i4.s    0A 
IL_0005:  stloc.1     
IL_0006:  ldloc.0     
IL_0007:  ldloc.1     
IL_0008:  div         
IL_0009:  stloc.2     
IL_000A:  ldloc.2     
IL_000B:  call        System.Console.WriteLine

The "proper" integer division in VB needs a backwards slash: p \ i

Solution 2

In VB, to do integer division, reverse the slash:

Dim y As Integer = p \ i

otherwise it is expanded to floating-point for the division, then forced back to an int after rounding when assigned to y.

Solution 3

VB.NET integer division operator is \, not /.

Solution 4

"Division is performed differently in C# and VB. C#, like other C-based languages truncates the division result when both operands are integer literals or integer variables (or integer constants). In VB, you must use the integer division operator (\) to get a similar result."

Source

Share:
12,458

Related videos on Youtube

Maxim Gershkovich
Author by

Maxim Gershkovich

Developer with experience in. ASP.NET Azure Point of sale software C# VB.NET .NET Framework Sharepoint MVC Microsoft Kinect for Windows 1.8 & 2

Updated on October 10, 2020

Comments

  • Maxim Gershkovich
    Maxim Gershkovich over 3 years

    Anyone care to explain why these two pieces of code exhibit different results?

    VB.NET v4.0

    Dim p As Integer = 16
    Dim i As Integer = 10
    Dim y As Integer = p / i
    //Result: 2
    

    C# v4.0

    int p = 16;
    int i = 10;
    int y = p / i;
    //Result: 1
    
    • BoltClock
      BoltClock almost 13 years
      It seems in VB.NET, p / i on two Integers results in a Double stackoverflow.com/questions/1953164/… which then gets squished to an Integer.
    • Shahab
      Shahab about 12 years
      If you're working in VB.NET you really should turn Option Strict on - then the compiler would have generated an error because of the loss of precision.
  • Rick Sladkey
    Rick Sladkey almost 13 years
    A refreshingly sophisticated answer to a simple question.
  • Sandeep G B
    Sandeep G B almost 13 years
    +1. Thanks for explaining it neatly :). Just curious to know if Microsoft is having plans to neutralize these language specific things in the future releases of .net?
  • Christian
    Christian almost 13 years
    @Sandeep: I don't think they'll change that behaviour as it not only works that way in VB.NET but also in previous versions. That means VB programmers are used to this behaviour and they actually expect it.
  • Cody Gray
    Cody Gray almost 13 years
    No, this is incorrect. See the other answers for details.
  • Cody Gray
    Cody Gray almost 13 years
    (In response to this justification offered by answerer in a now-deleted comment.) 1) That's inapplicable; it doesn't explain the behavior discussed in the question. 2) Why should I care that someone else on the Internet has the same misunderstanding? I can't downvote that answer anyway. 3) Even if this were correct, -1 for copying and pasting content from someone else's answer without attribution.
  • FIre Panda
    FIre Panda almost 13 years
    I came across with the post quite a while ago, so I aswered it and obviously thats not possible to remember the links of whatever you read on the internet.
  • Cody Gray
    Cody Gray almost 13 years
    Then it shouldn't be possible to remember the exact phrasing to use it in your answer. At any rate, now that you've updated your answer to match @Oded's, I'm not really sure what purpose it serves.
  • FIre Panda
    FIre Panda almost 13 years
    Well, I accept I should have verified the answer before posting.
  • MarkJ
    MarkJ almost 13 years
    +1. "This behaviour is by design" VB 10 language specification section 11.13.6
  • MarkJ
    MarkJ almost 13 years
    We could look at the VB language specification or the manual. In VB / always means floating-point division and `\` means integer division. IMHO it's quicker to learn VB by reading the manual rather than by reading the IL produced by the compiler.
  • SteveCinq
    SteveCinq over 5 years
    And why this isn't implemented in c# confounds me; clear, concise, useful. Probably 'they' just don't like the idea that VB has some elegance!