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.

65 posts
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. ...

Using Result<T> or OneOf<T> for Better Error Handling in .NET

Builds an ASP.NET Core AuthorService using Vladimir Khorikov's CSharpFunctionalExtensions library to return Result<T> instead of throwing exceptions for validation failures like a missing name or duplicate email, then contrasts it with OneOf<T> discriminated unions for several possible outcomes. ...

Lesser-known C# features that can simplify your code

Walks through concrete C# examples of records with value equality like a Coordinate type, the null-coalescing operator, tuple deconstruction for methods such as GenerateEmailContent, global usings that centralize imports across large projects, and C# 12's collection expressions to cut boilerplate. ...