Top 10 Key Features of .NET and Code Examples
.NET is a robust and versatile framework developed by Microsoft, enabling developers to build modern applications. Below are the top 10 features of .NET, explained with code examples to help you understand their application.
1. Cross-Platform Development
.NET Core allows developers to build and run applications on Windows, macOS, and Linux.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
Console.WriteLine($"Running on: {Environment.OSVersion}");
}
}
2. Unified Framework
.NET provides a unified platform for web, desktop, mobile, cloud, and IoT development.
using Microsoft.AspNetCore.Mvc;
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
3. High Performance
.NET Core offers improved performance with its optimized runtime and compilation.
ReadOnlySpan<char> span = "Hello, World!".AsSpan();
Console.WriteLine(span.Slice(0, 5).ToString()); // Output: Hello
4. Asynchronous Programming with async
and await
Simplifies writing non-blocking, asynchronous code.
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
using HttpClient client = new HttpClient();
string data = await client.GetStringAsync("https://example.com");
Console.WriteLine(data);
}
}
5. Dependency Injection (DI)
Built-in support for DI simplifies application scalability and testability.
public interface IMyService
{
string GetData();
}
public class MyService : IMyService
{
public string GetData() => "Hello from DI!";
}
// In Startup.cs
services.AddSingleton<IMyService, MyService>();
// In Controller
public class HomeController : Controller
{
private readonly IMyService _myService;
public HomeController(IMyService myService)
{
_myService = myService;
}
public IActionResult Index()
{
return Content(_myService.GetData());
}
}
6. Entity Framework Core (EF Core)
EF Core simplifies database access with object-relational mapping (ORM).
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// Querying data
using var context = new AppDbContext();
var products = context.Products.ToList();
7. Comprehensive Security
Provides secure development with features like authentication, authorization, and data protection.
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<AppDbContext>();
8. Blazor for WebAssembly
Blazor allows C# to be used for front-end development instead of JavaScript.
@page "/counter"
Counter
Current count: @count
@code {
private int count = 0;
private void IncrementCount()
{
count++;
}
}
9. Open Source
.NET is open source, enabling community-driven development and innovation. Explore and contribute on GitHub.
10. Rich Ecosystem and Tooling
.NET integrates seamlessly with tools like Visual Studio, Azure DevOps, and third-party libraries.
public static class HttpExample
{
[FunctionName("HttpExample")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Processing HTTP trigger.");
return new OkObjectResult("Hello, Azure Function!");
}
}
Conclusion
.NET's powerful features, such as cross-platform compatibility, high performance, and modern tooling, make it a go-to choice for developers worldwide. Whether you're building small applications or enterprise-grade systems, .NET offers the versatility and scalability required to meet diverse project needs.
No comments :
Post a Comment