C# Programming · Lesson 1

Creating Your First C# Console Application

Create, build, run, and understand a first C# console application with Visual Studio and .NET.

  • csharp
  • console-applications
  • visual-studio
  • dotnet
  • top-level-statements

Lesson purpose

Lesson 1 establishes the complete feedback loop for a C# console program: create a project, write source code, build it, run it, inspect the result, and revise it. Students also learn how C#, .NET, and Visual Studio work together, how to read the traditional application structure, and how top-level statements simplify short programs.

Learning objectives

  • Explain the roles of a program, C# source file, .NET, Visual Studio, project, solution, and console application.
  • Create a console project, edit its source file, build it, and run it.
  • Read the entry point, comments, statements, and basic console input/output.
  • Distinguish top-level statements from the traditional class-and-Main structure.
  • Save and reuse tested code with the Visual Studio Toolbox.
  • Choose a local IDE, an online editor, or a notebook according to the task.

1. Programs, C#, .NET, and Visual Studio

A program is a precise sequence of instructions that transforms input, state, or data into a result. A C# source file is a text file containing statements that follow C# grammar. The .cs extension identifies that source file. A successful build produces executable output.

C# is a high-level language. People write readable C# and the compiler translates it into instructions the computer can execute. The book describes C# as flexible, powerful, easier to use than lower-level C++ in many situations, visually capable through .NET libraries, internet-friendly, and designed with security in mind.

.NET supplies runtime services and libraries. The source distinguishes the Windows-focused .NET Framework from the cross-platform .NET Core family. For current work, students should recognize that modern .NET is cross-platform while older .NET Framework projects remain Windows-specific. Visual Studio is the integrated development environment that combines the editor, project system, compiler access, debugger, output windows, and configuration tools.

Ask students to identify which item is the language, which is the runtime and library platform, and which is the development environment.

2. Create, build, run, and inspect

A project is the folder and configuration that contains the files needed to build one application. A solution can contain one project or coordinate several related projects.

The Visual Studio sequence is:

  1. Open Visual Studio and choose Create a New Project.
  2. Filter for C#, Windows, and Console.
  3. Choose the appropriate Console App template.
  4. Name the project and select a location students can find.
  5. Keep the solution and project together for a simple first exercise.
  6. Edit Program.cs.
  7. Build the project, then run it without debugging or from a terminal.

Building compiles source and reports syntax or type errors. Running executes the built application and reveals behavior and output. A project can build successfully and still behave incorrectly, so students should separate compiler evidence from runtime evidence.

3. The first console program

The traditional framework makes the entry point explicit:

using System;

namespace Program1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your name, please:");
            string name = Console.ReadLine() ?? "";
            Console.WriteLine("Hello, " + name);
            Console.WriteLine("Press Enter to terminate...");
            Console.Read();
        }
    }
}

The using directive makes common .NET types available. A namespace groups names. A class contains program code. Main is the traditional starting method. Braces define blocks and a semicolon ends a statement.

WriteLine writes text and moves to a new line. ReadLine reads a line of text. ReadLine may return null, so the null-coalescing expression keeps name usable. Read pauses in environments where the window might close immediately.

Comments document intent without executing:

// The compiler ignores this explanatory text.
Console.WriteLine("Hello, C#!");  // This writes one line.

Use comments to explain decisions and assumptions rather than restating every obvious line. Visual Studio snippets such as cw can insert a WriteLine statement quickly.

4. Top-level statements

C# 9 and later allow a short console program to omit the namespace, class, and explicit Main method:

using System;

Console.WriteLine("Enter your name, please:");
string name = Console.ReadLine() ?? "";
Console.WriteLine($"Hello, {name}");
Console.WriteLine("Press Enter to terminate...");
Console.Read();

The compiler still creates an entry point behind the scenes. Top-level statements are useful for short demonstrations because the behavior is immediately visible. Explicit Main remains useful while teaching classes, methods, namespaces, or the traditional structure. The two forms express the same starting behavior with different levels of ceremony.

5. Reuse and alternate environments

The Toolbox workflow turns tested source into a reusable snippet: select useful lines, open and pin the Toolbox, drag or copy the selection into its General tab, and later insert it at the cursor in another project. This reduces routine typing while leaving students responsible for understanding the inserted code.

Online C# editors are useful for short experiments when Visual Studio is unavailable. They trade project structure and debugger access for quick access. Jupyter Notebook and .NET Interactive use a cell-based literate-programming style that mixes explanatory text with executable code. They are useful for exploration and reports but do not replace learning the project-and-build workflow.

Classroom application

Have students create a greeting program in both top-level and explicit Main forms. Require them to predict the output, build the project, run it, and record whether any problem appeared during editing, building, or execution.

Common misconceptions

  • C# is the language, .NET is the runtime and library platform, and Visual Studio is the IDE.
  • A successful build proves compilation, not correct behavior.
  • A comment is not an executable instruction.
  • Write and WriteLine differ in cursor movement.
  • ReadLine produces text and may return null.
  • A project and a solution are related containers, not interchangeable names.

Lesson summary

Students now have a repeatable create-edit-build-run-inspect cycle. They can identify the parts of a console application, use basic console I/O, read comments and statements, and choose between top-level and explicit Main syntax. The next lesson adds typed storage so programs can work with more than text.