← C# EnglishChapter 01 of 13

Introduction to C\#

## Learning Objectives - Understand C# history and .NET framework - Set up Visual Studio development environment - Write and run your first C# program - Understand C# syntax basics ## What is C#? C# (pronounced "C sharp") is a modern, object-oriented programming language developed by Microsoft as part of its .NET initiative. ```csharp using System; class HelloWorld { static void Main() { Console.WriteLine("Hello, World!"); } } ``` ## The .NET Framework ### Key Components | Component | Description | |-----------|-------------| | CLR | Common Language Runtime - executes C# code | | FCL | Framework Class Library - pre-built classes | | JIT | Just-In-Time compiler - compiles IL to native code | ### How .NET Works ```text C# Source Code ↓ Compiler ↓ IL (Intermediate Language) / MSIL ↓ JIT Compiler ↓ Native Machine Code ``` ### Key Features - **Type Safety** - Strong typing system - **Object-Oriented** - Everything except primitives is an object - **Automatic Memory Management** - Garbage collection - **Component-Oriented** - Built for reusable software - **Modern Syntax** - Clean, readable code ## Installing Visual Studio ### Download Get Visual Studio from ### Editions - **Community** - Free for individual developers - **Professional** - For professional developers - **Enterprise** - For large teams ### Workloads When installing, select: - **.NET desktop development** - **ASP.NET web development** ## Your First Program ### File: HelloWorld.cs ```csharp using System; class HelloWorld { static void Main() { Console.WriteLine("Hello, World!"); } } ``` ### Compile and Run ```bash dotnet new console -o HelloWorld cd HelloWorld dotnet run ``` ## C# Program Structure ```csharp using System; // Using directives namespace MyApp // Namespace (optional) { class Program // Class declaration { static void Main() // Main method - entry point { // Statements go here } } } ``` ### Components 1. **Using directives** - Import namespaces 2. **Namespace** - Organizes code logically 3. **Class** - Template for creating objects 4. **Main method** - Entry point of the program ## Top-Level Statements (C# 9+) Modern C# allows simplified program structure: ```csharp // Program.cs Console.WriteLine("Hello, World!"); ``` The compiler generates the class and Main method automatically. ## Comments ```csharp // Single-line comment /* * Multi-line comment */ /// /// XML documentation comment /// ``` ## The Main Method ### Traditional Syntax ```csharp static void Main() { // Code here } static void Main(string[] args) { foreach (string arg in args) { Console.WriteLine(arg); } } ``` ## Console Output ```csharp // Print with newline Console.WriteLine("Hello"); // Print without newline Console.Write("Hello "); Console.Write("World"); // String interpolation (C# 6+) string name = "Alice"; int age = 30; Console.WriteLine($"Name: {name}, Age: {age}"); // Formatted output Console.WriteLine("Name: {0}, Age: {1}", name, age); ``` ## Command Line Arguments ### Hello.cs ```csharp using System; class Hello { static void Main(string[] args) { if (args.Length > 0) { Console.WriteLine("Hello, " + args[0]); } else { Console.WriteLine("Hello, World!"); } } } ``` ```bash dotnet run -- Alice # Output: Hello, Alice ``` ## IDEs ### Popular IDEs - **Visual Studio** (recommended for Windows) - **Visual Studio Code** (cross-platform) - **JetBrains Rider** (cross-platform) - **Online Editors** (for quick testing) ### VS Code Extensions - C# for Visual Studio Code - .NET Install Tool ## C# Versions | Version | Year | Key Features | |---------|------|--------------| | C# 1.0 | 2002 | First release | | C# 2.0 | 2005 | Generics, nullable types | | C# 3.0 | 2007 | LINQ, lambda expressions | | C# 4.0 | 2010 | Dynamic binding | | C# 5.0 | 2012 | async/await | | C# 6.0 | 2015 | String interpolation, null-conditional | | C# 9.0 | 2020 | Records, pattern matching enhancements | | C# 10 | 2021 | Record structs, global using | | C# 11 | 2022 | Pattern matching improvements | | C# 12 | 2023 | Primary constructors, aliases | ## Checking Your Version ```bash dotnet --version ``` ## Summary - C# is a modern OOP language by Microsoft - .NET provides CLR and FCL - Code compiles to IL, then JIT compiles to native - Entry point is the Main method - Use `dotnet new console` to create a project - `dotnet run` compiles and executes - Comments: `//`, `/* */`, `///`

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →