1.4.16 Equality vs Assignment

= and == Have Different Jobs

C# uses similar-looking symbols for two very different operations.

Assignment
=

stores a value.

Equality comparison
==

asks whether two values are equal.

Keeping those operations separate is essential for reading and writing conditional logic.

Assignment Changes State

Consider:

player1.jerseyNumber = 7;

Read it as:

The statement changes the field's state.

The single equals sign does not ask a question.

Equality Produces a Boolean Result

Consider:

player1.jerseyNumber == 7

Read it as:

The expression evaluates to:

true

or:

false

That makes it suitable for an if condition.

Compare the Two Side by Side
Assignment
player1.isAvailable = true;

Meaning:

Equality
player1.isAvailable == true

Meaning:

The first changes state.

The second inspects state.

An if Condition Needs a Boolean Expression

For example:

if (player1.jerseyNumber == 7)
{
    System.Console.WriteLine("Number 7 selected.");
}

The condition:

player1.jerseyNumber == 7

asks a question whose answer is true or false.

That is exactly what the if statement needs.

A Boolean Field Can Often Be Used Directly

If:

player1.isAvailable

is already a Boolean field, this is clear:

if (player1.isAvailable)
{
    System.Console.WriteLine("Available");
}

You do not always need:

if (player1.isAvailable == true)

Both forms can communicate a Boolean condition, but the direct form is often easier to read.

Equality Can Compare Strings

You previously saw:

if (player1.position == "Goalkeeper")
{
    System.Console.WriteLine("Goalkeeper selected.");
}

The double equals sign compares the current string value with the expected string.

It does not assign "Goalkeeper" to the field.

Equality Can Compare References with null

You also saw:

if (player1.team == null)
{
    System.Console.WriteLine("No team assigned.");
}

Again:

==

asks a question.

It does not make the Team reference null.

Read the Operator Before Predicting the Result

When you encounter an equals-looking symbol, stop and identify it.

One equals sign
=

Think:

Two equals signs
==

Think:

This simple reading habit prevents many beginner mistakes.

Assignment Statements and Equality Expressions Can Work Together

For example:

player1.jerseyNumber = 7;

if (player1.jerseyNumber == 7)
{
    System.Console.WriteLine("Number confirmed.");
}

First, the program changes the field.

Then it evaluates whether the field equals 7.

The two statements have related values but different purposes.

The Main Distinction

Keep this sentence in mind:

Assignment changes a value. Equality compares values.

That distinction supports every conditional expression you write later.