C# Syntax and First Program
# 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, andMain().
-
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#:Let's break it down line by line:
| Component | Meaning |
|---|---|
using System; | Tells the compiler to use classes from the System namespace (which includes the Console class). |
namespace MyFirstApp | A logical container to organize your code and prevent naming conflicts. |
class Program | C# 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.
5. Comments in C#
Comments are notes in the code written for humans. The compiler completely ignores them.6. Mini Project: Simple Output Formatting
Let's build a program that prints a formatted receipt.7. OOP and Memory Explanations
When the CLR starts your program, it allocates memory and immediately seeks out theMain 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 asConsole.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.
Write a C# program that prints a triangle made of asterisks (
*) to the console using multipleConsole.WriteLine()statements.
- 2. Intentionally remove a semicolon from your program and try to run it. Observe the error message in Visual Studio.
11. MCQs with Answers
Which method acts as the entry point for a C# console application?
What character marks the end of a C# statement?
What does using System; do?
Which command prints text and leaves the cursor on the same line?
What is a namespace?
In classic C#, what must wrap the Main method?
Which feature introduced in newer .NET versions hides the boilerplate class Program and static void Main?
What happens if you forget a closing curly brace }?
12. Interview Questions
-
Q: Explain the purpose of the
statickeyword instatic void Main().
-
Q: What is the difference between
Console.WriteandConsole.WriteLine?
- Q: Why do we use namespaces in enterprise C# applications?
13. Summary
A C# program consists of a namespace, a class, and aMain 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.