<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pusparag subudhi</title>
    <description>The latest articles on DEV Community by pusparag subudhi (@pusparagsubudhi).</description>
    <link>https://dev.to/pusparagsubudhi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3584867%2F517af3f9-545a-4fef-b979-bbc6541d17c8.png</url>
      <title>DEV Community: pusparag subudhi</title>
      <link>https://dev.to/pusparagsubudhi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pusparagsubudhi"/>
    <language>en</language>
    <item>
      <title>C# Loop vs Linq. Does it really help</title>
      <dc:creator>pusparag subudhi</dc:creator>
      <pubDate>Mon, 27 Oct 2025 19:07:19 +0000</pubDate>
      <link>https://dev.to/pusparagsubudhi/c-loop-vs-linq-does-it-really-help-3ac3</link>
      <guid>https://dev.to/pusparagsubudhi/c-loop-vs-linq-does-it-really-help-3ac3</guid>
      <description>&lt;p&gt;C# Linq for collection has been long in the arena. It promises to make the code clean and minimalistic. Might be slightly less readable when you try to do a lot in less code. But then of comes the big question does it really solve the big issues related to "Green Code", is it really optimized.&lt;/p&gt;

&lt;p&gt;When you see a code like below you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static int GetLatestVersion(List&amp;lt;Event&amp;gt; events)
{
    int latestVersion = 0;
    foreach (Event event in events)
    {
        if (event.Version &amp;gt; latestVersion)
        {
            latestVersion = event.Version;
        }
    }
    return latestVersion;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first impulse would be to make life easier by switching to linq. Why have so many lines of code for something that can be written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static int GetLatestVersion(List&amp;lt;Event&amp;gt; events)
 {
     return events.Max(e =&amp;gt; e.Version);
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But wait, lets take a moments pause and evaluate the implication. By shifting the loop to linq are we making the code "Greener"? I couldn't find the relevant answer directly from google so decided to investigate myself. &lt;br&gt;
While linq does make the code simpler, it does add a huge performance overhead. Specially when working in applications that process millions of records in a day the performance impact is significant. There are a few old articles which suggested that performance would be better with newer versions of dotnet, but I don't see we are there yet!!&lt;/p&gt;

&lt;p&gt;After multiple trials i see that "Linq" code take &lt;strong&gt;3 times more&lt;/strong&gt; to find the max value in comparison to our plain old simple "for loop". The larger the data set the more exponential the problem would be. So the old loop wins in comparison to linq.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>discuss</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
