Ali Hamza Ansari

Ali Hamza Ansari

Meet Ali, a talented .NET developer from Pakistan. Besides blogging, Ali works for a range of international customers doing everything from C# to Azure.

67 posts
How .NET Garbage Collector works (and when you should care)

Explains the .NET Garbage Collector's mark, relocate, and compact phases on the managed heap and its Gen 0/1/2 generational model, showing why short-lived objects like loop variables are reclaimed cheaply in Gen 0 while longer-lived objects get promoted to slower, less frequent collections. ...

Hidden Costs of Boxing in C#: How to Detect and Avoid Them

Uses BenchmarkDotNet with MemoryDiagnoser to measure boxing a plain int into object, unboxing it back, and comparing both against an unboxed baseline, then extends the test to ArrayList versus List<int> to quantify how boxing's hidden heap allocations and GC pressure outweigh simple stack usage. ...

Expression Trees in C#: Building Dynamic LINQ Queries at Runtime

Shows how to build and compile C# expression trees at runtime using ParameterExpression, ConstantExpression, and Expression.Lambda to generate a working Func<int,int> delegate, then extends the technique to filter a Building entity's properties dynamically based on user-chosen search conditions. ...

Demystifying async void in C#: Why It's Dangerous and When It's Okay

Demonstrates with runnable C# code why an exception thrown inside an async void method crashes the app instead of being caught by a surrounding try/catch, unlike async Task, then walks through the compiler-generated state machine and AsyncTaskMethodBuilder that make Task methods exception-safe. ...

Visualizing LINQ Queries with LINQPad: Boost Your EF Core Debugging

Walks through installing LINQPad, created by Joe Albahari in 2007, connecting it to PostgreSQL and SQL Server databases, and using its interactive Database Explorer to drill into related tables like Albums and Tracks while inspecting the exact SQL generated behind an EF Core LINQ query. ...

Mastering Incremental Source Generators in C#: A Complete Guide with Example

Benchmarks JSON serialization of Blog and Course models three ways in C# using BenchmarkDotNet: a reflection-based serializer, a Roslyn source generator, and the .NET 6 incremental source generator, showing how caching compilation results cuts the overhead of repeated code generation. ...

How I Reduced API Response Time by 70% in a Large .NET Project

Applies 16 measured optimizations to a single ASP.NET Core PostsController endpoint over a 10,000-row PostgreSQL table, benchmarking each step from EF Core's AsNoTracking (up to 30% faster) through field projection and Skip/Take pagination to reach a cumulative 70% cut in response time. ...

Implementing Audit Logs in EF Core Without Polluting Your Entities

Builds audit logging into a PostgreSQL-backed ASP.NET Core job-portal API using an IAuditableEntity interface with CreatedAtUtc, UpdatedAtUtc, CreatedBy, and UpdatedBy on Applicant and Company entities, noting EF Core's shadow properties as an alternative that avoids adding those fields directly. ...

Exploring Source Generators in C#: Real-World Examples

Builds a Roslyn-based C# source generator from scratch, implementing ISourceGenerator's Initialize and Execute methods in a netstandard2.0 class library so a HelloSayenGenerator injects a working Console.WriteLine method into the compilation at build time with zero runtime cost. ...