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# — Boxing in Interfaces Slows Hot Paths

- 21.12.25 - ErcanOPAK comment on C# — Boxing in Interfaces Slows Hot Paths

void Log(IFormattable value) Passing structs through interfaces causes boxing. Fix Use generics: void Log<T>(T value) where T : IFormattable Result: zero allocations, faster execution.

Read More
C#

C# — WeakReference Prevents Hidden Memory Leaks

- 21.12.25 - ErcanOPAK comment on C# — WeakReference Prevents Hidden Memory Leaks

Event subscriptions can keep objects alive forever. var weak = new WeakReference<MyService>(service); Use case Caching Event listeners Plugin systems Prevents objects from surviving longer than intended.

Read More
C#

C# — ArrayPool: Stop Allocating, Start Reusing

- 21.12.25 - ErcanOPAK comment on C# — ArrayPool: Stop Allocating, Start Reusing

Allocating large arrays repeatedly kills performance. var pool = ArrayPool<byte>.Shared; var buffer = pool.Rent(4096); // use buffer pool.Return(buffer); Why this matters Reduces GC pressure dramatically Ideal for serializers, streams, IO-heavy code Used internally by ASP.NET Core ⚠ Always return the array — leaks are silent.

Read More
Asp.Net Core / C#

Request Body Can Only Be Read Once

- 19.12.25 - ErcanOPAK comment on Request Body Can Only Be Read Once

Middleware reading body breaks controllers. ✅ Fix Enable buffering: context.Request.EnableBuffering(); Then rewind stream.

Read More
Asp.Net Core / C#

Scoped Services Inside Singleton = Time Bomb

- 19.12.25 - ErcanOPAK comment on Scoped Services Inside Singleton = Time Bomb

public class MySingleton { public MySingleton(MyScopedService s) { } } ❌ Problem Captures first request scope forever Causes stale data & memory leaks ✅ Fix Use IServiceScopeFactory.

Read More
C#

Expression Trees Can Kill Startup Time

- 19.12.25 - ErcanOPAK comment on Expression Trees Can Kill Startup Time

Heavy use of expression compilation: Expression<Func<T, bool>> ❌ Cost JIT + dynamic method generation Slow cold starts ✅ Fix Cache compiled expressions aggressively.

Read More
C#

Why lock(this) Can Deadlock Your Entire App

- 19.12.25 - ErcanOPAK comment on Why lock(this) Can Deadlock Your Entire App

lock(this) { // work } Hidden problem External code can lock the same instance Causes unpredictable deadlocks ✅ Correct private readonly object _lock = new(); lock (_lock) { }  

Read More
C#

GC.AllocateUninitializedArray — Faster Arrays With a Catch

- 19.12.25 - ErcanOPAK comment on GC.AllocateUninitializedArray — Faster Arrays With a Catch

GC.AllocateUninitializedArray — Faster Arrays With a Catch Why it’s fast Skips zero-initialization Reduces CPU cycles ⚠ Danger Memory contains garbage values Must fully overwrite before use Use case High-performance serializers, buffers, pipelines.

Read More
C#

Equals() Without GetHashCode() Breaks HashSets

- 17.12.25 - ErcanOPAK comment on Equals() Without GetHashCode() Breaks HashSets

Overriding only Equals() is a bug factory. public override bool Equals(object obj) { … } ❌ Missing: public override int GetHashCode() { … } Result HashSet duplicates Dictionary lookups fail silently

Read More
C#

Why Dictionary Suddenly Becomes Slow

- 17.12.25 - ErcanOPAK comment on Why Dictionary Suddenly Becomes Slow

The hidden killer: rehashing. When capacity grows, the dictionary reallocates everything. ✅ Fix Always pre-size: var dict = new Dictionary<int, User>(expectedCount); Real impact Eliminates random latency spikes Crucial in hot paths

Read More
C#

Span — The Secret Weapon for Zero-Allocation Code

- 17.12.25 - ErcanOPAK comment on Span — The Secret Weapon for Zero-Allocation Code

Most high-performance .NET code avoids allocations entirely. Span<char> buffer = stackalloc char[64]; Why this matters Zero GC pressure Massive performance boost in parsing & serialization Used heavily inside .NET itself ⚠ Cannot escape the method scope.

Read More
Asp.Net Core / C#

Middleware Order Can Break Security

- 17.12.25 - ErcanOPAK comment on Middleware Order Can Break Security

Order matters a lot. ❌ Wrong UseEndpoints(); UseAuthentication(); ✅ Correct UseAuthentication(); UseAuthorization(); UseEndpoints();  

Read More
C#

Structs Aren’t Always Faster Than Classes

- 17.12.25 - ErcanOPAK comment on Structs Aren’t Always Faster Than Classes

Many devs assume structs = faster. ❌ Reality Large structs copy memory Passed by value by default ✅ Rule of Thumb Small, immutable → struct Complex objects → class

Read More
C# / LINQ

LINQ ToList() — The Performance Trap

- 17.12.25 - ErcanOPAK comment on LINQ ToList() — The Performance Trap

This line is expensive: var users = query.ToList(); 🧠 Why Executes immediately Loads everything into memory ✅ Better Stream or filter first: var users = query.Where(x => x.IsActive);  

Read More
C#

async void — The Exception Black Hole

- 17.12.25 - ErcanOPAK comment on async void — The Exception Black Hole

This should almost never exist: async void DoWork() ❌ Why it’s dangerous Exceptions crash the process Cannot be awaited Impossible to test ✅ Correct async Task DoWorkAsync() Only valid use: event handlers.

Read More
Asp.Net Core / C#

HttpClient Socket Exhaustion — Still Happening in 2025

- 16.12.25 - ErcanOPAK comment on HttpClient Socket Exhaustion — Still Happening in 2025

This is wrong: new HttpClient(); ✅ Correct builder.Services.AddHttpClient(); Why Reuses sockets Prevents random timeouts

Read More
Asp.Net Core / C#

Minimal APIs — Why Filters Replace Middleware

- 16.12.25 - ErcanOPAK comment on Minimal APIs — Why Filters Replace Middleware

Middleware runs for every request. ✅ Endpoint Filter app.MapPost(“/orders”, handler) .AddEndpointFilter<AuthFilter>(); Why Faster Scoped Cleaner

Read More
C#

ValueTask — Faster but Dangerous

- 16.12.25 - ErcanOPAK comment on ValueTask — Faster but Dangerous

ValueTask<int> GetAsync(); ⚠ Hidden traps Cannot await twice Harder debugging ✅ Rule Use only when result is usually synchronous.

Read More
C#

ConfigureAwait(false) — When It Actually Matters

- 16.12.25 - ErcanOPAK comment on ConfigureAwait(false) — When It Actually Matters

Using it everywhere is wrong. ✅ Use it when: Library code No UI / request context needed await httpClient.GetAsync(url) .ConfigureAwait(false); ❌ Avoid in: ASP.NET Core (no sync context) UI event handlers

Read More
C#

Task.Run() in ASP.NET — The Silent Scalability Killer

- 16.12.25 - ErcanOPAK comment on Task.Run() in ASP.NET — The Silent Scalability Killer

This looks harmless: Task.Run(() => DoWork()); ❌ Why it breaks production Bypasses request lifecycle Ignores cancellation Starves thread pool under load ✅ Correct Pattern Use background services: public class Worker : BackgroundService { protected override async Task ExecuteAsync(CancellationToken ct) { while (!ct.IsCancellationRequested) await DoWorkAsync(ct); } }  

Read More
Asp.Net Core / C#

ASP.NET Core “Request Body Already Read” — Enable Buffering

- 15.12.25 | 16.01.26 - ErcanOPAK comment on ASP.NET Core “Request Body Already Read” — Enable Buffering

Have you ever tried to read the HttpContext.Request.Body in a middleware or a filter, only to find that your API controller receives an empty body? Or worse, your application crashes? By default, the request body is a forward-only stream. Once it’s read, the pointer reaches the end, and there’s nothing left for the next component […]

Read More
Asp.Net Core / C#

ASP.NET Core “Response Compression Not Working” — The Missing MIME Types

- 15.12.25 - ErcanOPAK comment on ASP.NET Core “Response Compression Not Working” — The Missing MIME Types

Compression silently fails without explicit MIME config. ✅ Fix builder.Services.AddResponseCompression(options => { options.MimeTypes = ResponseCompressionDefaults.MimeTypes .Concat(new[] { “application/json” }); });  

Read More
C#

C# Boxing Kills Performance (Without You Noticing)

- 15.12.25 - ErcanOPAK comment on C# Boxing Kills Performance (Without You Noticing)

This looks harmless: List<object> list = new(); list.Add(5); // boxing ✅ Fix Use generics: List<int> list = new(); Why it matters Boxing allocates memory GC pressure increases fast in loops

Read More
C#

C# “Lock Contention” — Why lock(this) Breaks Apps

- 15.12.25 - ErcanOPAK comment on C# “Lock Contention” — Why lock(this) Breaks Apps

This is dangerous: lock(this) { // critical section } ❌ Why it fails External code can also lock on this Deadlocks appear randomly ✅ Correct Pattern private readonly object _sync = new(); lock (_sync) { // safe }  

Read More
C#

C# Async Streams — Stop Loading Huge Lists Into Memory

- 15.12.25 - ErcanOPAK comment on C# Async Streams — Stop Loading Huge Lists Into Memory

If you return large datasets like this: var data = await repository.GetAllAsync(); You already lost memory and latency. ✅ Stream Instead await foreach (var item in repository.StreamAsync()) { Process(item); } Why this is life-saving Near-zero memory footprint Faster first byte Perfect for logs, exports, ETL

Read More
Asp.Net Core / C#

ASP.NET Core Health Checks That Actually Matter

- 14.12.25 - ErcanOPAK comment on ASP.NET Core Health Checks That Actually Matter

Many health checks always return OK. ❌ Useless services.AddHealthChecks(); ✅ Useful services.AddHealthChecks() .AddSqlServer(conn) .AddRedis(redisConn); Why Kubernetes and load balancers rely on this.

Read More
Asp.Net Core / C#

ASP.NET Core “Slow Startup” — Reflection Scanning Trap

- 14.12.25 - ErcanOPAK comment on ASP.NET Core “Slow Startup” — Reflection Scanning Trap

Large projects often scan assemblies repeatedly. ✅ Fix Disable automatic scanning where possible and register explicitly: services.AddScoped<IService, Service>(); Why Reflection is slow at startup — especially in containers.

Read More
C#

C# Exception Handling — The Silent Performance Killer

- 14.12.25 - ErcanOPAK comment on C# Exception Handling — The Silent Performance Killer

Exceptions are expensive.They are not flow control. ❌ try { return dict[key]; } catch { return null; } ✅ return dict.TryGetValue(key, out var value) ? value : null; Rule If it can fail often → do NOT use exceptions.

Read More
C#

C# “foreach Is Slower Than for” — Yes, Sometimes

- 14.12.25 - ErcanOPAK comment on C# “foreach Is Slower Than for” — Yes, Sometimes

On List<T> this matters in tight loops. ❌ foreach (var item in list) { } ✅ Faster for (int i = 0; i < list.Count; i++) { } Why foreach creates an enumerator for is index-based and faster Use this only in hot paths, not everywhere.

Read More
C#

C# Span — The Secret to Zero-Allocation Performance

- 14.12.25 - ErcanOPAK comment on C# Span — The Secret to Zero-Allocation Performance

If you manipulate strings or byte arrays heavily, this matters. ❌ Common Allocation Trap var part = input.Substring(0, 10); ✅ Zero-Allocation Alternative ReadOnlySpan<char> part = input.AsSpan(0, 10); Why this is huge No heap allocation No GC pressure Perfect for parsers and hot paths

Read More
Page 9 of 14
« Previous 1 … 4 5 6 7 8 9 10 11 12 13 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 (836)
  • How to select distinct rows in a datatable in C# (805)
  • 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 (448)

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 (836)
  • How to select distinct rows in a datatable in C# (805)
  • 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