Review of Key Concepts
Reviewing key concepts is essential for reinforcing your understanding and ensuring that you can apply them effectively. This section will revisit some of the fundamental concepts in C# and provide detailed explanations and examples to solidify your knowledge.
1. Variables and Data Types
Variables are used to store data in a program. C# supports various data types, such as integers, floats, strings, and booleans. Understanding how to declare and use variables is crucial for writing efficient code.
Example
int age = 25; float price = 19.99f; string name = "John Doe"; bool isActive = true;
2. Control Structures
Control structures allow you to control the flow of your program. This includes conditional statements (if, else, switch) and loops (for, while, do-while). These structures help you make decisions and repeat actions based on certain conditions.
Example
if (age >= 18) { Console.WriteLine("You are an adult."); } else { Console.WriteLine("You are a minor."); } for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
3. Object-Oriented Programming (OOP)
OOP is a programming paradigm that uses objects and classes. Key concepts include encapsulation, inheritance, polymorphism, and abstraction. OOP helps in organizing code and making it reusable and maintainable.
Example
public class Animal { public string Name { get; set; } public void MakeSound() { Console.WriteLine("Animal sound"); } } public class Dog : Animal { public void Bark() { Console.WriteLine("Woof!"); } }
4. Methods and Functions
Methods are blocks of code that perform specific tasks. They can take parameters and return values. Understanding how to define and call methods is essential for writing modular and reusable code.
Example
public int Add(int a, int b) { return a + b; } int result = Add(5, 10); Console.WriteLine(result); // Output: 15
5. Exception Handling
Exception handling allows you to manage errors that occur during the execution of your program. Using try-catch blocks helps in gracefully handling exceptions and preventing the program from crashing.
Example
try { int[] numbers = { 1, 2, 3 }; Console.WriteLine(numbers[5]); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Index out of range: " + ex.Message); }
6. Arrays and Collections
Arrays and collections are used to store multiple values. Arrays are fixed in size, while collections like List, Dictionary, and HashSet are dynamic and offer more flexibility.
Example
int[] numbers = { 1, 2, 3, 4, 5 }; List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; Dictionary<int, string> students = new Dictionary<int, string> { { 1, "Alice" }, { 2, "Bob" }, { 3, "Charlie" } };
7. LINQ (Language Integrated Query)
LINQ provides a consistent way to query data from different data sources. It allows you to perform complex queries on collections and databases using a SQL-like syntax.
Example
var numbers = new List<int> { 1, 2, 3, 4, 5 }; var evenNumbers = from num in numbers where num % 2 == 0 select num; foreach (var num in evenNumbers) { Console.WriteLine(num); // Output: 2, 4 }
8. Multithreading
Multithreading allows you to perform multiple tasks concurrently. This is useful for improving the performance of CPU-bound and I/O-bound operations.
Example
public void DoWork() { for (int i = 0; i < 5; i++) { Console.WriteLine("Working..."); Thread.Sleep(1000); } } Thread workerThread = new Thread(DoWork); workerThread.Start();
9. Delegates and Events
Delegates are types that represent references to methods with a specific signature. Events are a way to notify subscribers when something happens. These concepts are useful for implementing callbacks and event-driven programming.
Example
public delegate void Notify(); public class Publisher { public event Notify EventOccurred; public void TriggerEvent() { EventOccurred?.Invoke(); } } public class Subscriber { public void OnEventOccurred() { Console.WriteLine("Event received!"); } } Publisher publisher = new Publisher(); Subscriber subscriber = new Subscriber(); publisher.EventOccurred += subscriber.OnEventOccurred; publisher.TriggerEvent();
10. File I/O
File I/O operations allow you to read from and write to files. Understanding how to handle files is essential for tasks like logging, data persistence, and configuration management.
Example
string path = "example.txt"; string content = "Hello, World!"; File.WriteAllText(path, content); string readContent = File.ReadAllText(path); Console.WriteLine(readContent); // Output: Hello, World!
11. Asynchronous Programming
Asynchronous programming allows you to perform time-consuming operations without blocking the main thread. This is particularly useful for improving the responsiveness of applications, especially those with a user interface.
Example
public async Task<string> DownloadDataAsync(string url) { using (HttpClient client = new HttpClient()) { return await client.GetStringAsync(url); } } string data = await DownloadDataAsync("https://example.com"); Console.WriteLine(data);