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
C#

Why foreach Is Sometimes Slower Than for

- 29.01.26 - ErcanOPAK

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]);
}

 

Related posts:

C# Exceptions Used for Flow Control

C# “Background Task Randomly Stops” — Lost Exceptions in Fire-and-Forget

EF Core Performance Boost: Use AsNoTracking() When Reading Data

Post Views: 1

Post navigation

The Hidden Cost of Exceptions as Flow Control
Why Visual Studio IntelliSense Gets Slower as Your Solution Grows

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

February 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
232425262728  
« Jan    

Most Viewed Posts

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

Recent Posts

  • AI Prompt: Simplify Legal Documents into Plain English
  • AI Prompt: Turn Meeting Notes into Actionable Task Lists
  • Docker: Use docker compose watch to Auto-Rebuild on File Changes
  • Kubernetes: View Real-Time Pod Logs with Stern Instead of kubectl
  • WordPress: Enable Maintenance Mode Without Plugins Using One Line of Code
  • WordPress: Add Custom Code Without Editing Theme Files Using Code Snippets Plugin
  • Photoshop: Convert Any Layer to Smart Object to Edit Non-Destructively
  • Photoshop: Use Content-Aware Fill to Remove Objects in Seconds
  • Visual Studio: Pin Variables While Debugging to Track Values Across Sessions
  • C#: Use Target-Typed new Expressions to Reduce Verbosity

Most Viewed Posts

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

Recent Posts

  • AI Prompt: Simplify Legal Documents into Plain English
  • AI Prompt: Turn Meeting Notes into Actionable Task Lists
  • Docker: Use docker compose watch to Auto-Rebuild on File Changes
  • Kubernetes: View Real-Time Pod Logs with Stern Instead of kubectl
  • WordPress: Enable Maintenance Mode Without Plugins Using One Line of Code

Social

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