C# / CHAPTER 01

Run and read a C# program

Beginner

Follow a small program, recognize statements, and print named values.

By the end of this chapter

  • Recognize a top-level C# program
  • Declare text and integer variables
  • Trace assignment and Console.WriteLine

The idea

A C# source file contains instructions that the compiler checks before execution. These lessons use the top-level statement style supported by modern C#. The using System; line makes the System namespace available, and Console.WriteLine displays a value followed by a new line. A semicolon ends each simple statement.

A declaration connects a type, a name, and an initial value. string title = "Practice"; stores text, while int completed = 2; stores a whole number. A later assignment can replace the value without redeclaring the variable. C# distinguishes uppercase and lowercase names, so completed and Completed are different identifiers.

Read statements in order and predict each output before running the example in a C# environment. This course introduces types, arrays, methods, and classes in sequence; you do not need to know typed collections before this first program. The next chapter explains more numeric and string operations.

C# example
using System;
string title = "Practice";
int completed = 2;
Console.WriteLine(title);
Console.WriteLine(completed);
completed = completed + 1;
Console.WriteLine(completed);
Result
Practice
2
3

The program prints the initial count, then prints its new value after the assignment adds one.

Try it yourself

Declare a string project name and an int task count. Print both, increase the count by two, and print it again.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

Which declaration stores a whole-number count?