Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 03 Beginner

C# Syntax and First Program

Updated: May 17, 2026
5 min read

# CHAPTER 3

C# Syntax and First Program

1. Introduction

Every programming language has grammar rules, known as syntax. C# is a C-family language, meaning its syntax is very similar to C, C++, and Java. It is strictly case-sensitive and relies on curly braces {} to define code blocks.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the basic structure of a C# program.
  • Explain the role of using, namespace, class, and Main().
  • Print text to the screen using Console.WriteLine().
  • Add single-line and multi-line comments to your code.

3. Dissecting "Hello World"

Let's look at the traditional first program in C#:
csharp
123456789101112
using System;

namespace MyFirstApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Let's break it down line by line:

ComponentMeaning
using System;Tells the compiler to use classes from the System namespace (which includes the Console class).
namespace MyFirstAppA logical container to organize your code and prevent naming conflicts.
class ProgramC# is highly Object-Oriented. Almost all code must live inside a class. This class is named Program.
static void Main(string[] args)The Entry Point of the application. When you run the app, the CLR looks for the Main method and executes the code inside it.
Console.WriteLine("Hello, World!");Prints the text to the screen and moves the cursor to the next line. Notice the semicolon (;) at the end!

*(Note: Starting in C# 9 / .NET 6, Microsoft introduced Top-Level Statements. You can now literally just write Console.WriteLine("Hello, World!"); in a new file, and the compiler will generate the namespace, class, and Main method for you behind the scenes. However, understanding the classic structure is essential for enterprise development).*

4. Printing Output

C# provides two main ways to print to the console:
  • Console.WriteLine(): Prints text and adds a newline at the end.
  • Console.Write(): Prints text but keeps the cursor on the same line.
csharp
123
Console.Write("Hello ");
Console.Write("World!");
// Output: Hello World!

5. Comments in C#

Comments are notes in the code written for humans. The compiler completely ignores them.
csharp
1234567
// This is a single-line comment. Useful for quick notes.

/* 
   This is a multi-line comment.
   It can span across several lines.
   Useful for temporarily disabling chunks of code.
*/

6. Mini Project: Simple Output Formatting

Let's build a program that prints a formatted receipt.
csharp
12345678910111213141516171819
using System;

namespace ReceiptPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("========================");
            Console.WriteLine("      STARBUCKS         ");
            Console.WriteLine("========================");
            Console.WriteLine("1x Coffee      $3.50");
            Console.WriteLine("1x Muffin      $2.00");
            Console.WriteLine("------------------------");
            Console.WriteLine("TOTAL:         $5.50");
            Console.WriteLine("========================");
        }
    }
}

7. OOP and Memory Explanations

When the CLR starts your program, it allocates memory and immediately seeks out the Main method. Because Main is marked static, the CLR does not need to create an object of the Program class in the Heap memory; it can call the method directly from the Class blueprint.

8. Common Mistakes

  • Forgetting the Semicolon: Every statement in C# must end with a ;. Missing this is the most common beginner error.
  • Case Sensitivity: console.writeline() will fail. It must be capitalized exactly as Console.WriteLine().
  • Mismatched Braces: Every opening brace { must have a corresponding closing brace }.

9. Best Practices

  • Always indent code inside curly braces. Visual Studio does this automatically, but keeping code clean is vital for readability.
  • Use comments to explain *why* you did something, not *what* you did.

10. Exercises

  1. 1. Write a C# program that prints a triangle made of asterisks (*) to the console using multiple Console.WriteLine() statements.
  1. 2. Intentionally remove a semicolon from your program and try to run it. Observe the error message in Visual Studio.

11. MCQs with Answers

Question 1

Which method acts as the entry point for a C# console application?

Question 2

What character marks the end of a C# statement?

Question 3

What does using System; do?

Q4. Is C# case-sensitive? a) Yes b) No Answer: a) Yes
Question 5

Which command prints text and leaves the cursor on the same line?

Q6. What symbol is used for a single-line comment? a) # b) <!-- --> c) /* d) // Answer: d) //
Question 7

What is a namespace?

Question 8

In classic C#, what must wrap the Main method?

Question 9

Which feature introduced in newer .NET versions hides the boilerplate class Program and static void Main?

Question 10

What happens if you forget a closing curly brace }?

12. Interview Questions

  • Q: Explain the purpose of the static keyword in static void Main().
  • Q: What is the difference between Console.Write and Console.WriteLine?
  • Q: Why do we use namespaces in enterprise C# applications?

13. Summary

A C# program consists of a namespace, a class, and a Main method where execution begins. The language is strict about case sensitivity, requires curly braces for scoping, and mandates semicolons at the end of statements. Console.WriteLine() is your primary tool for communicating with the user.

14. Next Chapter Recommendation

Now that we can print text, we need to learn how to store information. In Chapter 4: Variables and Data Types, we will explore how to store numbers, text, and true/false values in memory.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·