0) Goal of This Lesson
Operators define how values are calculated or combined,
and when multiple operators appear together, the order of evaluation matters.
This is not a memorization problem.
Itβs something you understand by running the code and observing the result.
1) The Most Basic Operators
In C#, the most fundamental arithmetic operators are:
-
+addition -
-subtraction -
*multiplication -
/division
βΆ Run this example
using System;
class Program
{
static void Main()
{
int a = 10;
int b = 5;
Console.WriteLine(a + b); // 15
Console.WriteLine(a - b); // 5
Console.WriteLine(a * b); // 50
Console.WriteLine(a / b); // 2
}
}
What to notice
-
int / intresults in an integer - The results match basic arithmetic expectations
2) Same Operator, Different Behavior Depending on Type
One important idea:
An operator can behave differently depending on the data type.
2-1) The + operator with strings
With numbers, + means addition.
With strings, + means concatenation.
using System;
class Program
{
static void Main()
{
string firstName = "Sabin";
string lastName = "Sim";
Console.WriteLine(firstName + " " + lastName);
}
}
Output
Sabin Sim
2-2) String + Number
This is also valid in C#.
using System;
class Program
{
static void Main()
{
int age = 36;
Console.WriteLine("Age: " + age);
}
}
Output
Age: 36
Why this works:
- The number is converted to a string
- Then the strings are concatenated
3) Why This Code Fails
Console.WriteLine("Result: " - 5);
π Compile-time error
Meaning:
C# does not know how to subtract a number from a string.
Important rule
-
string + intβ -
string - intβ -
string * intβ -
string / intβ
4) Operator Precedence
Now for a key concept:
When multiple operators appear in one expression,
C# follows a predefined order of evaluation.
This is called operator precedence.
For now, remember only this:
-
*and/first -
+and-later - Same precedence β evaluated left to right
5) Understanding Precedence by Running Code
5-1) Why does this work?
using System;
class Program
{
static void Main()
{
Console.WriteLine("Result: " + 10 * 2);
}
}
Output
Result: 20
Evaluation order
-
10 * 2β20 -
"Result: " + 20β string concatenation
5-2) Why does this fail?
Console.WriteLine("Result: " + 10 - 2);
π Compile-time error
What actually happens
-
"Result: " + 10β"Result: 10" -
"Result: 10" - 2β β invalid
6) Using Parentheses to Control Order
Parentheses tell the compiler exactly what to evaluate first.
6-1) Unexpected result
Console.WriteLine("Sum: " + 10 + 5);
Output
Sum: 105
Why?
-
"Sum: " + 10β"Sum: 10" -
"Sum: 10" + 5β"Sum: 105"
6-2) Correcting the intent
Console.WriteLine("Sum: " + (10 + 5));
Output
Sum: 15
π When strings and numbers are mixed, always think about parentheses.
7) Unary Operators (Single-Value Operators)
You only need to know two for now:
-
++increment -
--decrement
Example
using System;
class Program
{
static void Main()
{
int a = 10;
int b = 10;
a++; // a = a + 1
b--; // b = b - 1
Console.WriteLine(a); // 11
Console.WriteLine(b); // 9
}
}
8) What Not to Memorize
β Full operator precedence tables
β All possible operators
β Remember this instead:
When strings and numbers are mixed,
always question which part becomes a string first.
9) 30-Second Self Check
Explain the results without running the code:
Console.WriteLine("A" + 1 + 2);
Console.WriteLine("A" + (1 + 2));
If you can explain both results clearly,
π this lesson is complete.
Top comments (0)