DEV Community

Sabin Sim
Sabin Sim

Posted on

03. C# (Operators)

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
    }
}
Enter fullscreen mode Exit fullscreen mode

What to notice

  • int / int results 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Sabin Sim
Enter fullscreen mode Exit fullscreen mode

2-2) String + Number

This is also valid in C#.

using System;

class Program
{
    static void Main()
    {
        int age = 36;
        Console.WriteLine("Age: " + age);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Age: 36
Enter fullscreen mode Exit fullscreen mode

Why this works:

  • The number is converted to a string
  • Then the strings are concatenated

3) Why This Code Fails

Console.WriteLine("Result: " - 5);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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:

  1. * and / first
  2. + and - later
  3. 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Result: 20
Enter fullscreen mode Exit fullscreen mode

Evaluation order

  1. 10 * 2 β†’ 20
  2. "Result: " + 20 β†’ string concatenation

5-2) Why does this fail?

Console.WriteLine("Result: " + 10 - 2);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Compile-time error

What actually happens

  1. "Result: " + 10 β†’ "Result: 10"
  2. "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);
Enter fullscreen mode Exit fullscreen mode

Output

Sum: 105
Enter fullscreen mode Exit fullscreen mode

Why?

  • "Sum: " + 10 β†’ "Sum: 10"
  • "Sum: 10" + 5 β†’ "Sum: 105"

6-2) Correcting the intent

Console.WriteLine("Sum: " + (10 + 5));
Enter fullscreen mode Exit fullscreen mode

Output

Sum: 15
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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
    }
}
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

If you can explain both results clearly,
πŸ‘‰ this lesson is complete.

Top comments (0)