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#

C# String Interpolation: Use $”” for Readability, StringBuilder for Performance

- 01.02.26 - ErcanOPAK comment on C# String Interpolation: Use $”” for Readability, StringBuilder for Performance

Building strings in loops? String interpolation is clean but creates a new string on every concatenation. Here’s when to use what. The Readability Winner: // Old way – hard to read string message = “User ” + userName + ” (ID: ” + userId + “) logged in at ” + timestamp; // Modern way […]

Read More
C#

LINQ Performance Trap: Why .Any() Is 10,000x Faster Than .Count() > 0

- 01.02.26 - ErcanOPAK comment on LINQ Performance Trap: Why .Any() Is 10,000x Faster Than .Count() > 0

Using .Count() > 0 to check if a collection has items? You’re forcing LINQ to enumerate the entire collection just to answer a yes/no question. The Performance Killer: var users = dbContext.Users.Where(u => u.IsActive); // BAD: Counts ALL active users first if (users.Count() > 0) { Console.WriteLine(“We have active users”); } // Database query generated: […]

Read More
C#

C# Pattern Matching: Replace Complex If-Else Chains with Switch Expressions

- 01.02.26 - ErcanOPAK comment on C# Pattern Matching: Replace Complex If-Else Chains with Switch Expressions

Nested if-else statements making your code unreadable? C# 9+ switch expressions with pattern matching can reduce 50+ lines to 10. The Old Nightmare: public decimal CalculateDiscount(Customer customer, Order order) { if (customer == null) return 0; if (customer.IsPremium) { if (order.Total > 1000) return order.Total * 0.20m; else if (order.Total > 500) return order.Total * […]

Read More
C#

C# Records: Immutable Data Classes with Built-In Value Equality (C# 9+)

- 01.02.26 | 01.02.26 - ErcanOPAK comment on C# Records: Immutable Data Classes with Built-In Value Equality (C# 9+)

Tired of writing boilerplate Equals, GetHashCode, and ToString methods? C# records do it automatically with better performance. Old Way – Traditional Class: // BAD: 60+ lines for simple data class public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } […]

Read More
C#

C#: Async/Await Common Mistakes That Kill Performance (And How to Fix Them)

- 01.02.26 | 01.02.26 - ErcanOPAK comment on C#: Async/Await Common Mistakes That Kill Performance (And How to Fix Them)

Using async/await everywhere doesn’t automatically make your code faster – it can actually make it slower if done wrong. Here are the critical mistakes. Mistake 1: Async All The Way Down (Unnecessarily): // BAD: Unnecessary async overhead public async Task GetUserIdAsync(string username) { return await Task.FromResult(_cache.Get(username)); // Why async? } // This creates a Task, […]

Read More
C#

C# Performance: Use Span and Memory to Eliminate Array Allocations

- 01.02.26 | 01.02.26 - ErcanOPAK comment on C# Performance: Use Span and Memory to Eliminate Array Allocations

Processing large arrays or strings eating up memory? Span<T> lets you work with slices of memory without allocating new arrays. The Problem – Traditional Approach: // BAD: Creates new array on every call public byte[] ProcessChunk(byte[] data, int start, int length) { byte[] chunk = new byte[length]; // Heap allocation Array.Copy(data, start, chunk, 0, length); […]

Read More
Asp.Net Core / C#

.NET Core: Fix Memory Leaks by Understanding IDisposable and Using Patterns

- 01.02.26 | 01.02.26 - ErcanOPAK comment on .NET Core: Fix Memory Leaks by Understanding IDisposable and Using Patterns

Your .NET application’s memory usage grows infinitely? You’re probably not disposing resources properly. Here’s the complete guide. The Memory Leak – Classic Example: // WRONG: Memory leak public async Task ProcessOrdersAsync() { var dbContext = new ApplicationDbContext(); var orders = await dbContext.Orders.ToListAsync(); // Process orders… // dbContext never disposed = connection stays open = memory […]

Read More
C#

Why Exceptions Are Slower Than You Think

- 31.01.26 - ErcanOPAK comment on Why Exceptions Are Slower Than You Think

Exceptions are control-flow bombs. Cost Stack trace capture Heap allocations Cache pollution Rule Exceptions = exceptional cases. Alternative if (!TryParse(value, out result)) { // handle } Cause → Effect Using exceptions as logic → invisible performance debt.

Read More
C#

The Hidden Cost of record Types in Hot Paths

- 31.01.26 - ErcanOPAK comment on The Hidden Cost of record Types in Hot Paths

Records are great.Until you overuse them. Why Value-based equality Copy semantics More allocations in loops Rule Use records for: DTOs Messages Boundaries Avoid in: Hot loops Performance-critical paths

Read More
C#

Why async Code Can Still Block Threads

- 31.01.26 - ErcanOPAK comment on Why async Code Can Still Block Threads

Async ≠ non-blocking by default. The trap await Task.Run(() => LongCpuWork()); You just moved blocking to the thread pool. Rule Async is for I/O, not CPU. Correct Use async APIs for I/OUse background workers for CPU

Read More
Asp.Net Core / C#

Why BackgroundServices Die Silently in Production

- 31.01.26 - ErcanOPAK comment on Why BackgroundServices Die Silently in Production

Looks fine locally.Stops running in prod. Root cause Unhandled exceptions inside ExecuteAsync. What actually happens The host kills the service quietly. Correct pattern protected override async Task ExecuteAsync(CancellationToken ct) { while (!ct.IsCancellationRequested) { try { await DoWork(ct); } catch (Exception ex) { logger.LogError(ex, “Background task failed”); await Task.Delay(5000, ct); } } } Cause → Effect […]

Read More
C#

Why LINQ in Hot Paths Can Kill Performance

- 30.01.26 - ErcanOPAK comment on Why LINQ in Hot Paths Can Kill Performance

LINQ is elegant… and allocates. Problem Deferred execution Hidden enumerators Fix Use LINQ at boundaries, not inside loops. for (int i = 0; i < list.Count; i++) { Process(list[i]); } Why Predictable execution beats elegance under load.

Read More
C#

Why lock(this) Is a Concurrency Time Bomb

- 30.01.26 - ErcanOPAK comment on Why lock(this) Is a Concurrency Time Bomb

You don’t control who else locks this. Fix private readonly object _sync = new(); lock (_sync) { } Why Encapsulation matters in threading.

Read More
C#

Why async void Should Almost Never Exist

- 30.01.26 - ErcanOPAK comment on Why async void Should Almost Never Exist

async void hides exceptions. Only valid Event handlers Wrong async void Process() { throw new Exception(); } Correct async Task Process()  

Read More
Asp.Net Core / C#

Why Background Services Stop Without Errors

- 30.01.26 - ErcanOPAK comment on Why Background Services Stop Without Errors

BackgroundService silently dies if an exception escapes. Rule Always wrap execution loops. while (!stoppingToken.IsCancellationRequested) { try { await DoWork(); } catch (Exception ex) { Log(ex); } } Why Unhandled exceptions kill the worker.

Read More
Asp.Net Core / C#

Why HttpClient Causes Random Timeouts

- 30.01.26 - ErcanOPAK comment on Why HttpClient Causes Random Timeouts

Creating HttpClient per request is a silent killer. Problem Socket exhaustion DNS caching issues Fix Use IHttpClientFactory. services.AddHttpClient(); Why It pools handlers safely.

Read More
C#

Why foreach Is Sometimes Slower Than for

- 29.01.26 - ErcanOPAK comment on Why foreach Is Sometimes Slower Than for

Not always — but when it matters, it matters. Reason Enumerator allocations Interface dispatch Bounds checks differ Critical paths Use for on arrays and spans. for (int i = 0; i < arr.Length; i++) { Process(arr[i]); }  

Read More
C#

The Hidden Cost of Exceptions as Flow Control

- 29.01.26 - ErcanOPAK comment on The Hidden Cost of Exceptions as Flow Control

Exceptions are expensive. Bad try { Parse(input); } catch { } Better if (TryParse(input, out var result)) { … } Why Exceptions unwind stacks and allocate memory.

Read More
C#

Why lock Can Kill Throughput

- 29.01.26 - ErcanOPAK comment on Why lock Can Kill Throughput

Locks serialize execution. Bad lock(_sync) { Process(); } Better Reduce lock scope Prefer immutable data Use ConcurrentDictionary Why CPU cores idle while waiting.

Read More
Asp.Net Core / C#

The Real Cost of IHostedService Misuse

- 29.01.26 - ErcanOPAK comment on The Real Cost of IHostedService Misuse

Background services look harmless… until production. Common mistake Long-running loops No cancellation handling Blocking delays Correct pattern while (!stoppingToken.IsCancellationRequested) { await DoWorkAsync(stoppingToken); await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); } Why Graceful shutdowns save data and prevent zombie processes.

Read More
Asp.Net Core / C#

Why Async Controllers Still Block Threads

- 29.01.26 - ErcanOPAK comment on Why Async Controllers Still Block Threads

Async ≠ non-blocking. The trap public async Task<IActionResult> Get() { var data = _service.GetDataAsync().Result; return Ok(data); } Why it blocks .Result blocks the request thread Can cause thread starvation under load Fix var data = await _service.GetDataAsync();  

Read More
C#

Expression-Bodied Members

- 28.01.26 - ErcanOPAK comment on Expression-Bodied Members

int Sum() => a + b; Why it mattersReadable, compact, expressive.

Read More
C#

Span for High-Performance Parsing

- 28.01.26 - ErcanOPAK comment on Span for High-Performance Parsing

Why it mattersZero allocations in hot paths.

Read More
C#

Pattern Matching switch

- 28.01.26 - ErcanOPAK comment on Pattern Matching switch

return obj switch { null => 0, _ => 1 }; Why it mattersClear intent, fewer bugs.

Read More
Asp.Net Core / C#

Minimal APIs with Validation

- 28.01.26 - ErcanOPAK comment on Minimal APIs with Validation

app.MapPost(“/users”, (User u) => Results.Ok()); Why it mattersLess ceremony, same power.

Read More
C#

with Expressions for Safe Mutations

- 27.01.26 - ErcanOPAK comment on with Expressions for Safe Mutations

var updated = user with { IsActive = true }; Why it mattersImmutable style without boilerplate.

Read More
C#

Guard Clauses Improve Readability

- 27.01.26 - ErcanOPAK comment on Guard Clauses Improve Readability

if (user == null) return; Why it mattersFlatter code, easier reasoning.

Read More
C#

ValueTask for Hot Paths

- 27.01.26 - ErcanOPAK comment on ValueTask for Hot Paths

ValueTask<int> GetValueAsync(); Why it mattersAvoids allocations in high-frequency calls.

Read More
Asp.Net Core / C#

Use ProblemDetails for API Errors

- 27.01.26 - ErcanOPAK comment on Use ProblemDetails for API Errors

return Results.Problem(“Invalid input”); Why it mattersStandardized error responses = better clients.

Read More
Asp.Net Core / C#

Background Tasks with IHostedService

- 27.01.26 - ErcanOPAK comment on Background Tasks with IHostedService

public class Worker : BackgroundService { protected override Task ExecuteAsync(CancellationToken ct) => Task.CompletedTask; } Why it mattersClean background jobs without hacks.

Read More
Page 4 of 14
« Previous 1 2 3 4 5 6 7 8 9 … 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