C# Language Features
Explore the powerful and versatile features of the C# programming language, designed for building modern, robust, and scalable applications on the .NET platform.
Introduction to C#
C# is a modern, object-oriented, and type-safe programming language developed by Microsoft. It is designed for building a wide range of applications, from web services and desktop applications to mobile apps and games.
Key Language Features
- Type Safety: C# enforces strict type checking at compile time, helping to prevent common programming errors and improve code reliability.
- Object-Oriented Programming (OOP): Supports encapsulation, inheritance, and polymorphism, allowing for code reusability and modular design.
- Generics: Enables you to write type-safe code without sacrificing performance. Generic collections and methods improve code flexibility and efficiency.
-
Asynchronous Programming: Features like
async
andawait
simplify the development of responsive and scalable applications that perform I/O-bound operations without blocking the main thread. - LINQ (Language-Integrated Query): Provides a consistent syntax for querying data from various sources, including collections, databases, and XML.
- Delegates and Events: Fundamental for implementing callback mechanisms and event-driven programming.
- Attributes: Allow you to add declarative information to your code, which can be accessed at runtime via reflection.
- Extension Methods: Enable you to add new methods to existing types without modifying their source code.
- Pattern Matching: Advanced pattern matching capabilities in recent C# versions enhance code readability and expressiveness for conditional logic.
Example: Async/Await
The async
and await
keywords make it easier to write asynchronous code. Consider this example for downloading web content:
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class AsyncExample
{
public static async Task DownloadContentAsync(string url)
{
using (HttpClient client = new HttpClient())
{
try
{
Console.WriteLine($"Downloading from: {url}");
string content = await client.GetStringAsync(url);
Console.WriteLine("Download complete.");
// Process the content here
// Console.WriteLine($"Content length: {content.Length}");
}
catch (HttpRequestException e)
{
Console.WriteLine($"Error downloading content: {e.Message}");
}
}
}
public static async Task Main(string[] args)
{
string sampleUrl = "https://www.example.com";
await DownloadContentAsync(sampleUrl);
Console.WriteLine("Main method finished.");
}
}
Example: LINQ
LINQ simplifies data manipulation. Here's an example of filtering and sorting a list of numbers:
using System;
using System.Collections.Generic;
using System.Linq;
public class LinqExample
{
public static void Main(string[] args)
{
List numbers = new List { 5, 2, 8, 1, 9, 4, 7, 3, 6 };
// Filter for even numbers greater than 3, then sort them
var filteredAndSorted = numbers
.Where(n => n % 2 == 0 && n > 3)
.OrderBy(n => n)
.ToList();
Console.WriteLine("Filtered and sorted even numbers > 3:");
foreach (var number in filteredAndSorted)
{
Console.WriteLine(number);
}
}
}