Beginner

C# syntax and data types

Rules, names, strings, numbers, booleans, and the type habits that keep code predictable.

Chapter goal: Rules, names, strings, numbers, booleans, and the type habits that keep code predictable.

Simple explanation

Syntax is the grammar of code. A data type is a label that tells the program whether a value is text, a number, a true-or-false answer, or something more complex.

In C#, this chapter is about writing valid statements and choosing the right kind of data. Start with the idea above. Then connect each symbol to a value or action in the example.

Do not try to remember every symbol. First ask what data the program has, what it does with that data, and what result it creates. Technical words become easier when you connect them to those three questions.

Why this topic is important

Types tell the program what operations are allowed and help you catch mistakes early. In C#, the syntax may look different from other languages, but the thinking skill transfers: name the data, choose the right operation, and make the next step obvious.

When to use it

Use types whenever you store user input, scores, prices, settings, or any value that must behave consistently.

Example code

string title = "Starter Pack";
int lessons = 12;
bool published = true;
double rating = 4.8;

Line-by-line explanation

What the output means

No visible output: the example creates four typed values but does not print them.

The output is evidence that the program followed the instructions. If your result is different, read from the first line and write down how each value changes. That is debugging, not failure.

Mistake example

string age = "20";
int nextYear = age + 1; // Type error

The code mixes text and numeric work. A value that looks like a number can still be text, so the program must store or convert it correctly.

Fixed version

int age = 20;
int nextYear = age + 1;
Console.WriteLine(nextYear);

The corrected version stores age as a number, so adding one performs arithmetic.

Common mistakes

Warning: Change one part at a time. If you change many lines together, it becomes harder to learn which change caused the result.

Real use cases

Practice exercise

  1. Create one text, number, decimal, and boolean value.
  2. Convert numeric text before calculating with it.
  3. Explain why "5" and 5 behave differently.

Tip: If the exercise feels too large, complete only steps 1 to 3. Small working code teaches more than a large unfinished project.

Mini quiz

  1. Is "42" text or a number?
  2. Which type stores true or false?
  3. Why can a type conversion fail?

How to read AI-generated code

Do not copy AI code first. Read it like a detective. Find the data, follow the changes, and locate the final output. Ask AI to explain a line only after you have made your own guess.

Language reading check

Find the program entry point, follow function calls one at a time, and keep track of each value's type. Do not jump into a class or helper until you know who calls it.

Before you move on

Next topic

Next, learn variables and values. Before opening it, explain this chapter out loud in under one minute.

Open the interactive lesson →
← C# foundationsC# variables and values →