Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Operator Precedence


Share

Operator Precedence

When a calculation has more than one operator, C# must decide which part to calculate first. This is called operator precedence.

It works like the math you know from school: multiplication and division are done before addition and subtraction.

Example

int result = 2 + 3 * 4;
Console.WriteLine(result);  // 14

Try it Yourself »

C# first calculates 3 * 4, which is 12. Then it calculates 2 + 12, which is 14.


Parentheses Come First

If you want the addition to happen first, put it inside parentheses ( ). C# always calculates the part inside parentheses first:

Example

int result = (2 + 3) * 4;
Console.WriteLine(result);  // 20

Try it Yourself »

Now C# first calculates 2 + 3, which is 5. Then it calculates 5 * 4, which is 20.



Left to Right

When operators have the same precedence, like + and -, or * and /, C# calculates from left to right:

Example

int result1 = 10 - 2 + 5;  // (10 - 2) + 5 = 13
int result2 = 20 / 2 * 5;  // (20 / 2) * 5 = 50
Console.WriteLine(result1);
Console.WriteLine(result2);

Try it Yourself »


Order of Operations

The table below shows the most common operators. Operators at the top are calculated before operators further down:

Order Operator Description
1 ( ) Parentheses
2 ++ -- ! Increment, decrement and logical not
3 * / % Multiplication, division and modulus
4 + - Addition and subtraction
5 < > <= >= Less than and greater than (or equal to)
6 == != Equal to and not equal
7 && Logical and
8 || Logical or
9 = += -= *= /= Assignment

For example, + comes before >. So in the example below, C# first adds the numbers, and then compares the result:

Example

int points = 40;
int bonus = 20;
Console.WriteLine(points + bonus > 50);  // True

Try it Yourself »

points + bonus is 60, and 60 > 50 is True.

Assignment (=) is always last. That is why C# can calculate the whole right side first, and then store the result in the variable.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.