First C# Program
This chapter will guide you through creating and running your first C# program, understanding the basic structure of C# programs, and mastering the basic compilation and execution process.
Hello World Program
Creating the Project
Creating with Visual Studio
- Launch Visual Studio
- Select "Create new project"
- Choose "Console App" template
- Configure project information:
Creating with Command Line
Program Code
Visual Studio automatically generates the following code:
Understanding the Code Structure
Let's analyze each part of the program:
1. using Directive
- Purpose: Import the System namespace
- System namespace: Contains fundamental classes like Console
- using: Allows access to types without full namespace qualification
2. Namespace Declaration
- Namespace: Organizes code into logical groups
- HelloWorld: Custom namespace name
- Purpose: Prevents naming conflicts
3. Class Declaration
- Class: Blueprint for creating objects
- Program: Entry point class name
- Purpose: Contains the Main method
4. Main Method
- static: Method belongs to class, not instance
- void: Method returns no value
- Main: Entry point of the program
- string[] args: Command line arguments
Running the Program
Using Visual Studio
- Build the project: Press F6 or Build → Build Solution
- Run the program: Press F5 or Debug → Start Debugging
- View output: Console window will show "Hello World!"
Using Command Line
Output
Program Variations
Different Output Messages
Using String Interpolation
Reading User Input
Command Line Arguments
Run with arguments:
Output:
Modern C# Features
Top-Level Statements (C# 9.0+)
Global Using Directives (C# 10.0+)
Implicit Usings (C# 10.0+)
Compilation Process
What Happens When You Build
- Source Code: Your .cs files
- Compilation: C# compiler (csc.exe) converts to IL
- Intermediate Language: Platform-independent bytecode
- JIT Compilation: Just-In-Time compiler converts to machine code
- Execution: Machine code runs on target platform
Build Commands
Debugging Basics
Setting Breakpoints
- Click in margin: Click left margin of code line
- Press F9: Place cursor on line and press F9
- Conditional breakpoints: Right-click breakpoint → Condition
Debugging Steps
- Start debugging: Press F5
- Step over: Press F10 (execute current line)
- Step into: Press F11 (enter method)
- Continue: Press F5 (run to next breakpoint)
Debug Windows
- Autos: Variables used in current and previous lines
- Locals: Variables in current scope
- Watch: Custom expressions to monitor
- Call Stack: Method call hierarchy
Common Errors and Solutions
Syntax Errors
Compilation Errors
Runtime Errors
Best Practices
Code Organization
Error Handling
Summary
In this chapter, you learned:
- How to create a C# console application
- Basic structure of a C# program
- Key components: using directives, namespaces, classes, methods
- How to compile and run C# programs
- Modern C# features like top-level statements
- Basic debugging techniques
- Common errors and how to fix them
The Hello World program is your first step into C# programming. In the next chapter, we'll explore C# basic syntax in detail, including variables, data types, and operators.