Discussion:
What is the best place to ask question about c#
(too old to reply)
Luuk
2016-11-06 19:09:51 UTC
Permalink
What is the best place to ask question about c#?


And my second question is:

int number = 1;
try
{
number = number++ / 0;
}
catch { }
Console.WriteLine("number: {0}", number);


Why does this produce '2'?
I thought that the increment only took place *after* processing the
calculation of number/0, which obviously raises an exception.

In othe word, if i was doing this:
number = ++number / 0;

than i would have expected the result '2' ...
Arne Vajhøj
2016-11-06 23:00:30 UTC
Permalink
Post by Luuk
int number = 1;
try
{
number = number++ / 0;
}
catch { }
Console.WriteLine("number: {0}", number);
Why does this produce '2'?
I thought that the increment only took place *after* processing the
calculation of number/0, which obviously raises an exception.
I believe that:

number = number++ / 0;

can be executed as:

temp = number;
number++;
temp2 = temp / 0;
number = temp2;

and if so then the result 2 makes perfectly sense.

Arne

PS: I get a compile error, because the compiler detects that it
is divide by zero.
Arne Vajhøj
2016-11-06 23:05:49 UTC
Permalink
Post by Luuk
Post by Luuk
int number = 1;
try
{
number = number++ / 0;
}
catch { }
Console.WriteLine("number: {0}", number);
Why does this produce '2'?
I thought that the increment only took place *after* processing the
calculation of number/0, which obviously raises an exception.
number = number++ / 0;
temp = number;
number++;
temp2 = temp / 0;
number = temp2;
and if so then the result 2 makes perfectly sense.
I think "can be" is really "should be" as ++ has higher
precedence than /.

Arne
Luuk
2016-11-07 09:08:27 UTC
Permalink
Post by Arne Vajhøj
Post by Luuk
Post by Luuk
int number = 1;
try
{
number = number++ / 0;
}
catch { }
Console.WriteLine("number: {0}", number);
Why does this produce '2'?
I thought that the increment only took place *after* processing the
calculation of number/0, which obviously raises an exception.
number = number++ / 0;
temp = number;
number++;
temp2 = temp / 0;
number = temp2;
and if so then the result 2 makes perfectly sense.
I think "can be" is really "should be" as ++ has higher
precedence than /.
Arne
ok, higher precedence than /, but calculation is done on value before ++.

i think i know now that i should take care when typing stuff like this:
number = 2;
Console.WriteLine("number: {0}", number);
number = number++ / 1;
Console.WriteLine("number: {0}", number);
Console.WriteLine("number: {0}", (number++ / 1));
Console.WriteLine("number: {0}", number);

output:
2
2
2
3

;)
Arne Vajhøj
2016-11-08 01:09:17 UTC
Permalink
Post by Luuk
ok, higher precedence than /, but calculation is done on value before ++.
number = 2;
Console.WriteLine("number: {0}", number);
number = number++ / 1;
Console.WriteLine("number: {0}", number);
Console.WriteLine("number: {0}", (number++ / 1));
Console.WriteLine("number: {0}", number);
2
2
2
3
;)
The output is logical, but the code is not the most readable.

I tend to only use ++ and -- as standalone statements. Just
to avoid confusion.

Arne
Luuk
2016-11-12 09:36:47 UTC
Permalink
Post by Arne Vajhøj
Post by Luuk
ok, higher precedence than /, but calculation is done on value before ++.
number = 2;
Console.WriteLine("number: {0}", number);
number = number++ / 1;
Console.WriteLine("number: {0}", number);
Console.WriteLine("number: {0}", (number++ / 1));
Console.WriteLine("number: {0}", number);
2
2
2
3
;)
The output is logical, but the code is not the most readable.
The code is just some lines added because of 'what happens if i do this'.
The resulting code is therfore just a short test, for which i did know
what i wanted to test last week, but do not know now.... ;)
Post by Arne Vajhøj
I tend to only use ++ and -- as standalone statements. Just
to avoid confusion.
I'm on the learning curve, thanks for the help!
Post by Arne Vajhøj
Arne
Arne Vajhøj
2016-11-13 00:58:47 UTC
Permalink
Post by Luuk
Post by Arne Vajhøj
I tend to only use ++ and -- as standalone statements. Just
to avoid confusion.
I'm on the learning curve, thanks for the help!
In general - if a piece of code require the reader to think
about language details (like operator precedence), then the code
should be rewritten (if possible).

Arne
Luuk
2016-11-07 09:12:08 UTC
Permalink
Post by Luuk
Post by Luuk
int number = 1;
try
{
number = number++ / 0;
}
catch { }
Console.WriteLine("number: {0}", number);
Why does this produce '2'?
I thought that the increment only took place *after* processing the
calculation of number/0, which obviously raises an exception.
number = number++ / 0;
temp = number;
number++;
temp2 = temp / 0;
number = temp2;
and if so then the result 2 makes perfectly sense.
Arne
PS: I get a compile error, because the compiler detects that it
is divide by zero.
My compiler (Visual Studio 2015) does not give that error when compiling
on this:
number = number++ / 0;
but it will give the error on:
number = 42 / 0;
Obviously because of compile optimizations.
Arne Vajhøj
2016-11-08 00:55:35 UTC
Permalink
Post by Luuk
Post by Arne Vajhøj
Post by Luuk
int number = 1;
try
{
number = number++ / 0;
}
catch { }
Console.WriteLine("number: {0}", number);
PS: I get a compile error, because the compiler detects that it
is divide by zero.
My compiler (Visual Studio 2015) does not give that error when compiling
Hm.

VS 2010:

C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 4.6.1590.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework,
but only supports language versions up to C# 5, which is no longer the
latest version. For compilers that support newer versions of the C#
programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

e.cs(10,22): error CS0020: Division by constant zero

C:\Work>

VS 2012:

C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 4.6.1590.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework,
but only supports language versions up to C# 5, which is no longer the
latest version. For compilers that support newer versions of the C#
programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

e.cs(10,22): error CS0020: Division by constant zero

C:\Work>

VS 2013:

C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 12.0.31101.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

e.cs(10,22): error CS0020: Division by constant zero

C:\Work>

VS 2015:

C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 1.0.0.50618
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Work>

I guess somebody changed something in 2015.

Arne
Luuk
2016-11-12 10:43:03 UTC
Permalink
Post by Arne Vajhøj
Post by Luuk
Post by Arne Vajhøj
Post by Luuk
int number = 1;
try
{
number = number++ / 0;
}
catch { }
Console.WriteLine("number: {0}", number);
PS: I get a compile error, because the compiler detects that it
is divide by zero.
My compiler (Visual Studio 2015) does not give that error when compiling
Hm.
C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 4.6.1590.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework,
but only supports language versions up to C# 5, which is no longer the
latest version. For compilers that support newer versions of the C#
programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
e.cs(10,22): error CS0020: Division by constant zero
C:\Work>
C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 4.6.1590.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework,
but only supports language versions up to C# 5, which is no longer the
latest version. For compilers that support newer versions of the C#
programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
e.cs(10,22): error CS0020: Division by constant zero
C:\Work>
C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 12.0.31101.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
e.cs(10,22): error CS0020: Division by constant zero
C:\Work>
C:\Work>csc e.cs
Microsoft (R) Visual C# Compiler version 1.0.0.50618
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Work>
I guess somebody changed something in 2015.
Arne
"If the value of the right operand is zero, a
System.DivideByZeroException is thrown."
from this document (about 5.0):
CSharp Language Specification.docx
https://www.microsoft.com/en-us/download/confirmation.aspx?id=7029



But something is REALLY wrong....

The example from this location:
https://msdn.microsoft.com/en-us/library/system.dividebyzeroexception(v=vs.110).aspx

//EXAMPLE
using System;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int number1 = 3000;
int number2 = 0;

Console.WriteLine((double)number1 / number2);
}
}
}
//END EXAMPLE

produces the answer '8' when compiles with .NET Frameword 4 (or above)
and 'Infinity' with .NET Frameword 3.5 (and below) ....

This was tested with Visual Studio Community 2015
Version 14.0.25431.01 Update 3
Luuk
2016-11-12 17:23:09 UTC
Permalink
Post by Luuk
But something is REALLY wrong....
Correction:
It seems to be not wrong, but really _strange_ to give an output of '8'
to a calculation when there really should be an error (or 'Infinity')
Arne Vajhøj
2016-11-13 03:52:17 UTC
Permalink
Post by Luuk
Post by Arne Vajhøj
I guess somebody changed something in 2015.
"If the value of the right operand is zero, a
System.DivideByZeroException is thrown."
CSharp Language Specification.docx
That is the required runtime behavior and what will always
happen with a variable.

Not sure if that technically prohibits the compiler from
flagging with a constant zero.
Post by Luuk
But something is REALLY wrong....
https://msdn.microsoft.com/en-us/library/system.dividebyzeroexception(v=vs.110).aspx
//EXAMPLE
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int number1 = 3000;
int number2 = 0;
Console.WriteLine((double)number1 / number2);
}
}
}
//END EXAMPLE
produces the answer '8' when compiles with .NET Frameword 4 (or above)
and 'Infinity' with .NET Frameword 3.5 (and below) ....
This was tested with Visual Studio Community 2015
Version 14.0.25431.01 Update 3
Well - we went from integer division to floating point
division.

But I can not recreate. Even after updating to Update 3.

Before update:

C:\Work>csc z.cs
Microsoft (R) Visual C# Compiler version 1.0.0.50618
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Work>z
Infinity

After update:

C:\Work>csc z.cs
Microsoft (R) Visual C# Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Work>z
Infinity

Arne

Arne Vajhøj
2016-11-06 23:02:10 UTC
Permalink
Post by Luuk
What is the best place to ask question about c#?
For usenet then this it the correct group.

But there are also other possibilities:
- MS got some web fora
- StackOverflow got some good C# people
etc.

Arne
Continue reading on narkive:
Loading...