22

If you create new projects in C# and VB.NET, then go directly in the Immediate Window and type this:

? 567 / 1000

C# will return 0, while VB.NET will return 0.567.

To get the same result in C#, you need to type

? 567 / 1000.0

Why is there this difference? Why does C# require the explicit decimal point after 1000?

10
  • 4
    Amazing! You type "/" in MSDN index, select "/ operator [C#]" and read from the 3rd sentence: When you divide two integers, the result is always an integer. For example, the result of 5 / 2 is 2. To determine the remainder of 5 / 2, use the modulo operator (%). To obtain a quotient as a rational number or fraction, give the dividend or divisor type float or type double. You can do this implicitly by putting a decimal point after the number, as shown in the following example. I can't believe there's somebody who votes for such a question, answeres on it and votes for answers on it. Dec 23, 2009 at 14:58
  • 7
    DTashkinov: Apparently, you should read the FAQ of StackOverflow. Dec 23, 2009 at 15:09
  • 3
    @Dtashkinov I agree 100%. Basic searching and learning is no longer expected. Post anything you like and vote. Missing a semi-colon? No problem just post it on stackoverflow we can help you. It is ridiculious!
    – JonH
    Dec 23, 2009 at 15:16
  • 2
    Good question, I originally was very strong with C# but due to my current job only use VB.NET, glad you mentioned this as otherwise I'd go back to C# and wonder why division wasn't working the same! Dec 23, 2009 at 15:16
  • 3
    Hmm, maybe I was too pationate, sorry. But discussing what is clearly written in documentation looks strange to me. Dec 23, 2009 at 15:22

6 Answers 6

35

The / operator in C# for integer operands does the "integer division" operation (equivalent to \ operator in VB.NET). For VB.NET, it's the "normal" division (will give fractional result). In C#, in order to do that, you'll have to cast at least one operand to a floating point type (e.g. double) explicitly.

4
  • 3
    My guess is that the reason this is different in VB (from C#), is because VB.Net was originally spec'd to retain as much syntactical similarilty to VB6 as possible, so as not alienate the large VB6 user base that existed at the time. And that's the way it worked in VB6. Dec 23, 2009 at 15:14
  • @Charles: Yeah, both languages have a long heritage: The way C# works is the way C works and I'm sure / and \ operators behaved this way at least since QBasic days (I guess this was the case since earlier BASICs but that's the first thing I've worked with). Dec 23, 2009 at 15:19
  • 4
    Just in case the poster needed to know how to do it, in C#: 567.0 / 1000.0; or (double)567 / 1000; or even 567 / (double)1000; will give you what you need. As long as one of the numbers is a double it will perform floating point division.
    – BlueTrin
    Dec 23, 2009 at 15:40
  • I struggled and used CType for both operands of \ in VB.Net until I found this article. Mar 12, 2011 at 17:20
11

Because in VB.NET, the / operator is defined to return a floating-point result. It widens its inputs to double and performs the division. In C#, the / operator performs integer division when both inputs are integers.

See MSDN for VB.NET.

Divides two numbers and returns a floating-point result.

Before division is performed, any integral numeric expressions are widened to Double.

See MSDN for C#.

The division operator (/) divides its first operand by its second. All numeric types have predefined division operators.

When you divide two integers, the result is always an integer.

To get the same semantics in VB.NET as the / operator on integers in C#, use the \ operator.

Divides two numbers and returns an integer result.

0
3

By default C# is treating 576 / 1000 as integer division so you get an integer as the result.

In VB.NET it's treating it as floating point division.

By adding ".0" on a number in C# you are explicitly telling it this number is a floating point number and hence the division becomes floating point as well.

2

The languages are different. In C# the compiler interprets those numbers as integers and uses integer division. In VB.NET the compiler uses floating point division.

2

C# Language Reference

VB.Net Language Reference

VB.Net has both a / and a \ operator. / happens to be the floating point division operator.

0

C#'s / equalant in VB.Net is \

C#.Net:

/ Operator Divides two numbers and returns an integer result.

VB.Net:

/ Operator Divides two numbers and returns a floating-point result.

\ Operator Divides two numbers and returns an integer result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.