Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Category: C#

C#

Records for Configuration Objects

- 26.01.26 - ErcanOPAK comment on Records for Configuration Objects

public record AppConfig(string Url, int Timeout); Why it mattersImmutable configs = fewer runtime surprises.

Read More
C#

Use Span to Avoid Allocations

- 26.01.26 - ErcanOPAK comment on Use Span to Avoid Allocations

Span<char> buffer = stackalloc char[128]; Why it mattersHigh-performance without GC pressure.

Read More
C#

Pattern Matching for Cleaner Logic

- 26.01.26 - ErcanOPAK comment on Pattern Matching for Cleaner Logic

if (obj is User { IsActive: true }) Why it mattersReadable, safe, expressive code.

Read More
Asp.Net Core / C#

Use MapGroup for Clean Endpoints

- 26.01.26 - ErcanOPAK comment on Use MapGroup for Clean Endpoints

app.MapGroup(“/api/users”) .MapGet(“/”, GetUsers); Why it mattersBetter structure without controllers.

Read More
Asp.Net Core / C#

Why Minimal APIs Improve Cold Start

- 26.01.26 - ErcanOPAK comment on Why Minimal APIs Improve Cold Start

app.MapGet(“/ping”, () => “pong”); Why it mattersLess middleware, faster startup.

Read More
C#

Stop Boxing With in Parameters for Hot Paths

- 25.01.26 - ErcanOPAK comment on Stop Boxing With in Parameters for Hot Paths

void Process(in LargeStruct data) Why this mattersAvoids copies and reduces GC pressure.

Read More
C#

Use FrozenDictionary for Read-Heavy Workloads (.NET 8+)

- 25.01.26 - ErcanOPAK comment on Use FrozenDictionary for Read-Heavy Workloads (.NET 8+)

var map = myDict.ToFrozenDictionary(); Why this mattersOptimized lookups, thread-safe, zero mutation cost.

Read More
C#

Why record Types Reduce Bugs in DTOs

- 25.01.26 | 15.02.26 - ErcanOPAK comment on Why record Types Reduce Bugs in DTOs

The Problem: How many times have you created a simple class just to hold data (like a DTO or a config object), only to find yourself drowning in boilerplate code? You need properties, a constructor, and if you want to compare them, you have to override Equals() and GetHashCode(). Before you know it, a simple […]

Read More
Asp.Net Core / C#

Use IHttpClientFactory or Face Socket Exhaustion

- 25.01.26 - ErcanOPAK comment on Use IHttpClientFactory or Face Socket Exhaustion

Creating HttpClient per request is a silent killer. services.AddHttpClient(); Why this mattersConnection pooling prevents random production outages.

Read More
Asp.Net Core / C#

Why BackgroundService Beats Task.Run in APIs

- 25.01.26 - ErcanOPAK comment on Why BackgroundService Beats Task.Run in APIs

Fire-and-forget tasks die with requests. public class Worker : BackgroundService { protected override async Task ExecuteAsync(CancellationToken ct) { while (!ct.IsCancellationRequested) await DoWorkAsync(ct); } } Why this mattersHosted services respect app lifetime and graceful shutdown.

Read More
C#

Why Exceptions Should NOT Be Used for Flow Control

- 24.01.26 - ErcanOPAK comment on Why Exceptions Should NOT Be Used for Flow Control

if (!dict.TryGetValue(key, out value)) return; Why this mattersExceptions are expensive and destroy hot paths.

Read More
C#

Span — High Performance Without Unsafe Code

- 24.01.26 - ErcanOPAK comment on Span — High Performance Without Unsafe Code

Span<char> buffer = stackalloc char[128]; Why this mattersZero allocations, cache-friendly, safe memory access.

Read More
C#

Why ValueTask Exists (And When NOT to Use It)

- 24.01.26 - ErcanOPAK comment on Why ValueTask Exists (And When NOT to Use It)

ValueTask reduces allocations — only when results are often synchronous. ValueTask<int> GetCachedAsync() Why it’s dangerousMisuse increases complexity and bugs.

Read More
Asp.Net Core / C#

Use IOptionsSnapshot to Fix Config Reload Issues

- 24.01.26 - ErcanOPAK comment on Use IOptionsSnapshot to Fix Config Reload Issues

Reading config directly causes stale values. public MyService(IOptionsSnapshot<MyConfig> cfg) { _cfg = cfg.Value; } Why this mattersSupports per-request refresh in scoped services.

Read More
Asp.Net Core / C#

Why Async Controllers Improve Throughput (Not Speed)

- 24.01.26 - ErcanOPAK comment on Why Async Controllers Improve Throughput (Not Speed)

Async doesn’t make code faster — it makes servers scalable. public async Task<IActionResult> Get() { var data = await repo.GetAsync(); return Ok(data); } Why this mattersThreads are freed while waiting → more concurrent requests.

Read More
C#

Avoid “new” in Loops with Object Pooling

- 24.01.26 - ErcanOPAK comment on Avoid “new” in Loops with Object Pooling

Heavy loops + object creation = GC pressure. var pool = new ObjectPool<MyDto>(() => new MyDto()); var obj = pool.Get(); // use obj pool.Return(obj); Why it works: Reuses memory Reduces Gen0/Gen1 collections Stabilizes latency Perfect for: Parsers Serializers Message processing This is how you make C# behave like systems code.

Read More
C#

Span Can Replace 80% of Your String Allocations

- 24.01.26 - ErcanOPAK comment on Span Can Replace 80% of Your String Allocations

If you process strings heavily, Span<T> is a silent performance killer… in a good way. ReadOnlySpan<char> input = “order:12345”.AsSpan(); var idPart = input.Slice(input.IndexOf(‘:’) + 1); int id = int.Parse(idPart); Why this is powerful: No substring allocations No temporary strings Works directly on memory When to use it:Parsing, slicing, validation, protocol handling. When NOT to:Async methods […]

Read More
C#

ValueTask Is Not a Faster Task (Unless You Use It Right)

- 24.01.26 - ErcanOPAK comment on ValueTask Is Not a Faster Task (Unless You Use It Right)

Most developers replace Task with ValueTask expecting instant performance gains.The truth: misusing ValueTask can actually make your code slower and more fragile. ValueTask shines only when: The result is often synchronous Allocation pressure matters (high-throughput paths) public ValueTask<int> GetCachedValueAsync() { if (_cache.TryGetValue(“x”, out int value)) return new ValueTask<int>(value); return new ValueTask<int>(LoadFromDbAsync()); } Why it matters:ValueTask […]

Read More
Asp.Net Core / C#

Why IAsyncEnumerable Is a Game-Changer for Data Streaming APIs

- 24.01.26 - ErcanOPAK comment on Why IAsyncEnumerable Is a Game-Changer for Data Streaming APIs

Returning large datasets blocks memory.Streaming them changes the entire performance profile. public async IAsyncEnumerable<int> StreamNumbers() { for (int i = 0; i < 1000; i++) { await Task.Delay(10); yield return i; } } Why it works Consumers process data as it arrives Lower memory footprint Faster perceived response times Perfect for: Reporting APIs Log streaming […]

Read More
C#

The Hidden Cost of async Without await in Real Applications

- 24.01.26 - ErcanOPAK comment on The Hidden Cost of async Without await in Real Applications

An async method without await still creates a state machine.That means overhead without benefit. public async Task<int> CalculateAsync() { return 42; } Why this matters: Extra allocations Confusing stack traces No real async benefit Better: public Task<int> CalculateAsync() { return Task.FromResult(42); } Cause → Effect Unnecessary async → hidden performance tax Explicit tasks → clearer […]

Read More
C#

Why Span Can Quietly Make Your C# Code Faster (Without Unsafe Code)

- 24.01.26 - ErcanOPAK comment on Why Span Can Quietly Make Your C# Code Faster (Without Unsafe Code)

Most C# performance gains come from avoiding allocations, not from writing complex algorithms.Span<T> lets you work with slices of memory without creating new objects, which means less GC pressure and smoother performance. Why it matters:String slicing, parsing, or buffer manipulation usually creates hidden allocations. Span<T> works directly on memory. ReadOnlySpan<char> span = “Invoice-2026-0198”.AsSpan(); var year […]

Read More
Asp.Net Core / C#

Stop Wasting RAM: The Art of Zero-Allocation C#

- 23.01.26 - ErcanOPAK comment on Stop Wasting RAM: The Art of Zero-Allocation C#

How to achieve extreme performance in .NET 9 by mastering Span<T>, Memory<T>, and the Garbage Collector. In the world of cloud-native development, Memory is Money. Every byte you allocate on the Managed Heap is a debt that the Garbage Collector (GC) must eventually collect. The biggest bottleneck in high-throughput .NET applications isn’t typically the CPU’s […]

Read More
Asp.Net Core / C#

Is Your Clean Architecture Actually a “Dirty” Mess?

- 23.01.26 - ErcanOPAK comment on Is Your Clean Architecture Actually a “Dirty” Mess?

Why modern .NET 9 systems are moving away from rigid layers and embracing the “Vertical Slice” revolution. For the past decade, Clean Architecture (or Onion Architecture) has been the gold standard for .NET developers. We’ve been told to separate our concerns into layers: Domain, Application, Infrastructure, and Web. But as projects grow, we often find […]

Read More
Asp.Net Core / C#

The Most Expensive Mistake C# Developers Still Make in 2026

- 23.01.26 | 23.01.26 - ErcanOPAK comment on The Most Expensive Mistake C# Developers Still Make in 2026

How a single line of Task.Run can turn your high-performance C# application into a production time bomb. For years, C# has been marketed as a “safe” and “high-level” language. Garbage collection, async/await, dependency injection — all designed to protect developers from low-level mistakes. And yet… Some of the most expensive production failures I’ve seen in […]

Read More
C#

C# Value Types Passed Incorrectly

- 23.01.26 - ErcanOPAK comment on C# Value Types Passed Incorrectly

Unexpected copies. Why it happensMissing in keyword. Why it mattersPerf degradation. Vital fix void Process(in LargeStruct data)  

Read More
C#

C# LINQ Looks Clean but Allocates Heavily

- 23.01.26 - ErcanOPAK comment on C# LINQ Looks Clean but Allocates Heavily

Readable but slow. Why it happensDeferred execution + allocations. Why it mattersGC pressure. Vital fix Use loops in hot paths.

Read More
C#

C# Exceptions Kill Performance Silently

- 23.01.26 - ErcanOPAK comment on C# Exceptions Kill Performance Silently

No crash, but slow. Why it happensExceptions used as control flow. Why it mattersHidden overhead. Vital fix Avoid exceptions in hot paths.

Read More
C#

C# Dictionary Lookups Are Slower Than Expected

- 22.01.26 - ErcanOPAK comment on C# Dictionary Lookups Are Slower Than Expected

O(1) isn’t always fast. Why it happensBad hash distribution. Why it mattersHigh-frequency code suffers. Smart fixUse value types with good hash codes.

Read More
C#

C# Records Cause Unexpected Copies

- 22.01.26 - ErcanOPAK comment on C# Records Cause Unexpected Copies

Performance drops silently. Why it happensValue-based equality. Why it mattersLarge objects get copied. Smart fixUse records for small immutable models only.

Read More
C#

C# Async Methods Look Async But Aren’t

- 22.01.26 - ErcanOPAK comment on C# Async Methods Look Async But Aren’t

Await everywhere, still blocking. Why it happensSynchronous IO inside async methods. Why it mattersThread starvation. Smart fixUse true async APIs only.

Read More
Page 5 of 14
« Previous 1 2 3 4 5 6 7 8 9 10 … 14 Next »

Posts navigation

Older posts
Newer posts
April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (837)
  • How to select distinct rows in a datatable in C# (806)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (449)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (837)
  • How to select distinct rows in a datatable in C# (806)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com