Skip to content

Order of precedence and associativity #34283

@ahtello

Description

@ahtello

Code example:
'private int year = 2023;
private static int ToDecadeBeginning(this int year) => year / 10 * 10;'

According to order of precedence of operators and operator associativity in this documentation,
I think this expression would evaluate to 2023 / (10 * 10) = 20.
Instead it evaluates to (2023 / 10) * 10 = 2020.

Associativity of this precedence level:

x * y, x / y, x % y Multiplicative
'*' and '/' are at the same precedence level, but with the left-to-right associativity I believe '10*10' would be evaluated before '2023/10'? What am I missing here? Why does '2023/10' get evaluated before '10*10'?

Can I get an explanation of why, or may it be a bug in VS Studio (2022 and Code)?

image
image


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions