Overview
The .NET platform provides a comprehensive and consistent programming model for building applications that have visually stunning user experiences, seamless and secure communication, and the ability to model a range of business processes.
Key components:
- Common Language Runtime (CLR)
- Base Class Library (BCL)
- Multi-language support (C#, F#, VB.NET)
- Cross‑platform capabilities
Language Basics
.NET supports several languages, with C# being the most popular. Below is a simple Hello World program:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, .NET!");
}
}
Key concepts include variables, control flow, classes, and methods.
Data Types
.NET provides both value and reference types.
int number = 42; // Value type
string text = "Hello World"; // Reference type
bool flag = true;
double pi = 3.14159;
DateTime now = DateTime.Now;
Nullable types allow value types to represent undefined values:
int? nullableInt = null;
Memory Management
.NET uses a garbage collector (GC) to automatically manage memory. Developers can influence GC behavior using using
statements, Dispose
, and weak references.
using (var stream = new FileStream("data.txt", FileMode.Open))
{
// Stream is automatically closed
}
Async & Await
Asynchronous programming improves responsiveness. The async
and await
keywords simplify working with tasks.
using System.Net.Http;
using System.Threading.Tasks;
public async Task GetContentAsync(string url)
{
using HttpClient client = new HttpClient();
return await client.GetStringAsync(url);
}
Example usage:
string html = await GetContentAsync("https://example.com");
Console.WriteLine(html);
Best Practices
- Prefer
async
all the way down. - Use
using
for IDisposable objects. - Favor immutable types where possible.
- Leverage dependency injection for testability.
- Write unit tests with xUnit or NUnit.