<?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>Forem: Common Khadka</title>
    <description>The latest articles on Forem by Common Khadka (@silentcoder52626).</description>
    <link>https://forem.com/silentcoder52626</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%2F266679%2Fa6443b7a-70ce-4e1b-a7d7-ba153060ae45.jpeg</url>
      <title>Forem: Common Khadka</title>
      <link>https://forem.com/silentcoder52626</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/silentcoder52626"/>
    <language>en</language>
    <item>
      <title>Software Architecture for a CHILD</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Tue, 08 Apr 2025 17:37:33 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/software-architecture-for-a-child-5e20</link>
      <guid>https://forem.com/silentcoder52626/software-architecture-for-a-child-5e20</guid>
      <description>&lt;h3&gt;
  
  
  🏠 &lt;strong&gt;What is Software Architecture?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you're building a house or a LEGO city. You need a &lt;strong&gt;plan&lt;/strong&gt; — where the rooms go, how people move around, where the wires and pipes are. That’s what &lt;strong&gt;software architecture&lt;/strong&gt; is — the &lt;strong&gt;big plan&lt;/strong&gt; for how a software project is built and how all the parts fit together.&lt;/p&gt;




&lt;p&gt;Now let’s look at the &lt;strong&gt;types of software architecture&lt;/strong&gt; using easy examples:&lt;/p&gt;




&lt;h3&gt;
  
  
  1. &lt;strong&gt;Monolithic Architecture 🧱&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Imagine a giant LEGO castle all built as one big piece.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All rooms are in one big structure.&lt;/li&gt;
&lt;li&gt;If you want to change the kitchen, you might break the whole castle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🟢 Easy to build at first.&lt;br&gt;&lt;br&gt;
🔴 Hard to change when it gets big.&lt;/p&gt;

&lt;p&gt;🧠 Example: A single app that does everything — login, shop, pay — all in one codebase.&lt;/p&gt;



&lt;p&gt;All in one folder — like a big box of LEGOs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/MyApp
├── /controllers
├── /models
├── /views
├── /services
├── /utils
├── app.js
├── config.js
└── README.md

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Everything is tightly packed together. Easy at first, but hard to move pieces around later.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Microservices Architecture 🏘️&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Imagine a neighborhood where each house does one thing.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One house for cooking.&lt;/li&gt;
&lt;li&gt;One house for games.&lt;/li&gt;
&lt;li&gt;One house for sleeping.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each house talks to the others, but they can be fixed or upgraded separately.&lt;/p&gt;

&lt;p&gt;🟢 Easy to manage and scale.&lt;br&gt;&lt;br&gt;
🔴 Harder to set up at first.&lt;/p&gt;

&lt;p&gt;🧠 Example: Amazon has one service just for payments, one for searching, &lt;br&gt;
one for orders.&lt;/p&gt;

&lt;p&gt;Many small folders, each like its own tiny app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/MyApp
├── /auth-service
│   ├── /src
│   ├── app.js
│   └── Dockerfile
├── /product-service
│   ├── /src
│   ├── app.js
│   └── Dockerfile
├── /order-service
│   ├── /src
│   ├── app.js
│   └── Dockerfile
├── /api-gateway
│   ├── /src
│   └── gateway.js
└── docker-compose.yml

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Each service is independent and talks to others through APIs.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;Clean Architecture 🧼 (or Hexagonal)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Imagine a house where the living room doesn’t know where the pipes are.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The inside (like the people) don't care about the outside stuff (like the street or electricity).&lt;/li&gt;
&lt;li&gt;You can cleanly replace the TV or the power supply without messing up the couch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🟢 Keeps core logic safe from changes.&lt;br&gt;&lt;br&gt;
🔴 Takes more planning.&lt;/p&gt;
&lt;h2&gt;
  
  
  🧠 Example: Your game logic doesn’t care whether the player uses keyboard or touch screen.
&lt;/h2&gt;

&lt;p&gt;Separates the “what” from the “how” — like keeping your school books in subjects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/MyApp
├── /src
│   ├── /core
│   │   ├── /entities
│   │   ├── /use-cases
│   ├── /infrastructure
│   │   ├── /database
│   │   ├── /external-apis
│   ├── /interface
│   │   ├── /controllers
│   │   └── /routes
├── app.js
└── README.md

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Core logic stays clean and isolated — like the rules of your game that don’t depend on who’s playing.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  4. &lt;strong&gt;Onion Architecture 🧅&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Imagine layers of an onion — each layer protects the one inside.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The center is your &lt;strong&gt;core logic&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Around that is stuff like &lt;strong&gt;business rules&lt;/strong&gt;, then &lt;strong&gt;APIs&lt;/strong&gt;, then &lt;strong&gt;UI&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only outer layers talk to inner layers — not the other way around.&lt;/p&gt;

&lt;p&gt;🟢 Makes the important code safe from changes outside.&lt;br&gt;&lt;br&gt;
🔴 Can be hard to understand at first.&lt;/p&gt;

&lt;p&gt;🧠 Example: You can change the way data is saved (database) without touching your game rules.&lt;/p&gt;



&lt;p&gt;Layers that protect the core, like a security system around your treasure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/MyApp
├── /Domain
│   └── /Entities
├── /Application
│   └── /UseCases
├── /Infrastructure
│   ├── /Data
│   └── /External
├── /Presentation
│   └── /Controllers
├── app.js
└── README.md

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Outer layers (like controllers) depend on the inner logic, but never the other way around.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  5. &lt;strong&gt;Layered Architecture 🍰&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Like a cake with layers — base, cream, topping.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each layer does one job:

&lt;ul&gt;
&lt;li&gt;Data layer (bottom): like the fridge with all the food.&lt;/li&gt;
&lt;li&gt;Logic layer (middle): the cook that makes the food.&lt;/li&gt;
&lt;li&gt;UI layer (top): the waiter that serves it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;🟢 Easy to organize and understand.&lt;br&gt;&lt;br&gt;
🔴 Sometimes layers talk too much and get messy.&lt;/p&gt;

&lt;p&gt;🧠 Example: Your app’s UI doesn’t need to know how the database works — it just asks for stuff.&lt;/p&gt;



&lt;p&gt;Stacked like a birthday cake 🎂 — base, middle, and top layers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/MyApp
├── /PresentationLayer
│   └── /UI
├── /BusinessLogicLayer
│   └── /Services
├── /DataAccessLayer
│   └── /Repositories
├── /Common
│   └── /Helpers
├── app.js
└── README.md

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Each layer only talks to the one right below or above it. Like a kitchen: waiter → chef → fridge.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  💡 In short:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Like a...&lt;/th&gt;
&lt;th&gt;Good for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Monolithic&lt;/td&gt;
&lt;td&gt;One big LEGO block&lt;/td&gt;
&lt;td&gt;Small apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microservices&lt;/td&gt;
&lt;td&gt;Tiny houses in a city&lt;/td&gt;
&lt;td&gt;Large, fast-growing apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Clean&lt;/td&gt;
&lt;td&gt;Tidy and independent rooms&lt;/td&gt;
&lt;td&gt;Testable, flexible code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Onion&lt;/td&gt;
&lt;td&gt;Onion with strong core&lt;/td&gt;
&lt;td&gt;Safe core logic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Layered&lt;/td&gt;
&lt;td&gt;Layer cake&lt;/td&gt;
&lt;td&gt;Simple and organized apps&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




</description>
      <category>programming</category>
      <category>softwareengineering</category>
      <category>architecture</category>
    </item>
    <item>
      <title>PLINQ Extension Methods in C#</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Tue, 11 Feb 2025 17:37:48 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/plinq-extension-methods-in-c-1p6o</link>
      <guid>https://forem.com/silentcoder52626/plinq-extension-methods-in-c-1p6o</guid>
      <description>&lt;p&gt;Parallel LINQ (PLINQ) is an extension of LINQ that enables parallel processing of queries to improve performance by utilizing multiple cores of a processor. Below is a list of PLINQ extension methods along with code snippets demonstrating their usage.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. AsParallel
&lt;/h2&gt;

&lt;p&gt;Converts an &lt;code&gt;IEnumerable&lt;/code&gt; to a parallel query, enabling parallel execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;parallelQuery&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;parallelQuery&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. WithDegreeOfParallelism
&lt;/h2&gt;

&lt;p&gt;Limits the number of processor cores used for the query execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;parallelQuery&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                           &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WithDegreeOfParallelism&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Limits to 2 cores&lt;/span&gt;
                           &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;parallelQuery&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. ParallelEnumerable.Range
&lt;/h2&gt;

&lt;p&gt;Generates a range of numbers in parallel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;parallelRange&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ParallelEnumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;parallelRange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  4. ParallelEnumerable.Repeat
&lt;/h2&gt;

&lt;p&gt;Generates a sequence where a specified value is repeated multiple times in parallel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;repeatedValues&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ParallelEnumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;repeatedValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5. ParallelEnumerable.Empty
&lt;/h2&gt;

&lt;p&gt;Returns an empty parallel sequence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;emptySequence&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ParallelEnumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Count: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;emptySequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  6. AsSequential
&lt;/h2&gt;

&lt;p&gt;Forces a PLINQ query to process sequentially instead of in parallel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sequentialQuery&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AsSequential&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sequentialQuery&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  7. AsOrdered
&lt;/h2&gt;

&lt;p&gt;Preserves the ordering of the original sequence in a PLINQ query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;AsOrdered&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  8. AsUnordered
&lt;/h2&gt;

&lt;p&gt;Removes ordering constraints for better parallel performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;AsUnordered&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  9. WithCancellation
&lt;/h2&gt;

&lt;p&gt;Enables cancellation of a PLINQ query using a &lt;code&gt;CancellationToken&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;cts&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CancellationTokenSource&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;WithCancellation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Cancel&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OperationCanceledException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Query was canceled."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  10. WithMergeOptions
&lt;/h2&gt;

&lt;p&gt;Specifies how results should be merged after parallel execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WithMergeOptions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ParallelMergeOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NotBuffered&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  11. WithExecutionMode
&lt;/h2&gt;

&lt;p&gt;Forces a query to run in parallel or sequential mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WithExecutionMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ParallelExecutionMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ForceParallelism&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  12. ForAll
&lt;/h2&gt;

&lt;p&gt;Executes a parallel query and applies an action to each element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;AsParallel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;PLINQ provides powerful methods to enable efficient parallel processing in C#. Understanding these methods can help improve performance in applications that handle large data processing tasks.&lt;/p&gt;




&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/parallel-linq-plinq" rel="noopener noreferrer"&gt;Microsoft Docs: PLINQ Overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>csharp</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding LINQ Extension Methods in C#</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Mon, 10 Feb 2025 15:59:21 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/understanding-linq-extension-methods-in-c-15kh</link>
      <guid>https://forem.com/silentcoder52626/understanding-linq-extension-methods-in-c-15kh</guid>
      <description>&lt;p&gt;In C#, Language Integrated Query (LINQ) is a powerful feature that allows developers to query and manipulate data in a more concise and readable manner. One of the key concepts in LINQ is the use of &lt;strong&gt;extension methods&lt;/strong&gt;. These methods are used to extend the functionality of existing types, enabling you to query data sources in a fluent, declarative style. In this blog post, we’ll dive into what LINQ extension methods are, why they’re important, and how to use them effectively.&lt;/p&gt;




&lt;h4&gt;
  
  
  What are LINQ Extension Methods?
&lt;/h4&gt;

&lt;p&gt;LINQ extension methods are methods that are added to types like &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; and &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt; to enable LINQ-style querying. These methods are defined in the &lt;code&gt;System.Linq&lt;/code&gt; namespace and are part of the &lt;code&gt;Enumerable&lt;/code&gt; and &lt;code&gt;Queryable&lt;/code&gt; classes. They allow developers to perform operations such as filtering, grouping, sorting, and projecting data directly from collections like arrays, lists, or databases.&lt;/p&gt;

&lt;p&gt;Although these methods are not explicitly part of the &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt; interfaces, they are made available through C#’s &lt;strong&gt;extension method&lt;/strong&gt; feature. By using extension methods, you can call LINQ methods like &lt;code&gt;Where()&lt;/code&gt;, &lt;code&gt;Select()&lt;/code&gt;, &lt;code&gt;OrderBy()&lt;/code&gt;, and &lt;code&gt;GroupBy()&lt;/code&gt; directly on collections that implement &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  Importance of LINQ Extension Methods
&lt;/h4&gt;

&lt;p&gt;LINQ extension methods significantly improve the readability, maintainability, and efficiency of your code. Here are a few reasons why they are important:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Declarative Syntax&lt;/strong&gt;: LINQ provides a declarative syntax for querying collections. Instead of writing complex loops, you can describe the logic of your query in a way that closely resembles the structure of SQL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Increased Productivity&lt;/strong&gt;: LINQ simplifies the code you need to write, reducing the chances of errors. With LINQ extension methods, you can perform operations like filtering, ordering, and projecting data in just one line of code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cleaner and More Readable Code&lt;/strong&gt;: Using LINQ extension methods makes your code concise and easier to understand. Rather than dealing with low-level iteration or complex conditional logic, you can focus on the core business logic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Extensibility&lt;/strong&gt;: Since LINQ methods are based on extension methods, you can extend the functionality of any type that implements &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt;, making your code flexible and extensible.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  How to Use LINQ Extension Methods
&lt;/h4&gt;

&lt;p&gt;To use LINQ extension methods, you must first import the &lt;code&gt;System.Linq&lt;/code&gt; namespace. After that, you can apply various methods to collections. Let’s look at some common examples:&lt;/p&gt;

&lt;h5&gt;
  
  
  1. &lt;strong&gt;Filtering Data: &lt;code&gt;Where()&lt;/code&gt;&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;Where()&lt;/code&gt; method filters a sequence based on a predicate (a boolean expression). Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Linq&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;evenNumbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;evenNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
4
6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;Where()&lt;/code&gt; is used to filter out the even numbers from the array.&lt;/p&gt;

&lt;h5&gt;
  
  
  2. &lt;strong&gt;Projecting Data: &lt;code&gt;Select()&lt;/code&gt;&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;Select()&lt;/code&gt; method projects each element of a collection into a new form. Here’s an example that transforms each number into its square:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;squaredNumbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;squaredNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
4
9
16
25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  3. &lt;strong&gt;Sorting Data: &lt;code&gt;OrderBy()&lt;/code&gt;&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;OrderBy()&lt;/code&gt; method sorts the elements of a collection in ascending order. You can also use &lt;code&gt;OrderByDescending()&lt;/code&gt; for descending order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sortedNumbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;OrderBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sortedNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
3
4
5
8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  4. &lt;strong&gt;Grouping Data: &lt;code&gt;GroupBy()&lt;/code&gt;&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;GroupBy()&lt;/code&gt; method is used to group elements based on a key. Here's an example of grouping a list of people by their age:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Mike"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Sarah"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;groupedByAge&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GroupBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;groupedByAge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Age: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"- &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Age: 25
- John
- Mike
Age: 30
- Jane
- Sarah
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  More LINQ Extension Methods
&lt;/h4&gt;

&lt;p&gt;The above methods are just a few examples of the many powerful LINQ extension methods available in C#. There are several other methods that can help you perform more complex operations like joining collections, checking for conditions, concatenating sequences, and more.&lt;/p&gt;

&lt;p&gt;To explore these additional methods, I have included more examples and detailed explanations in my GitHub repository. You can check out the full list of LINQ extension methods along with code snippets by visiting my repository:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/SilentCoder52626/LINQ-Extensions/blob/main/README.md" rel="noopener noreferrer"&gt;LINQ Extension Methods - GitHub Repository&lt;/a&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;LINQ extension methods are a crucial tool in C# for making code more efficient, readable, and maintainable. They provide an intuitive way to query, filter, transform, and manipulate data. By using LINQ, developers can avoid writing verbose and error-prone loops, reducing the complexity of their code. Whether you're working with in-memory collections, databases, or remote data sources, LINQ extension methods offer a simple yet powerful way to handle data in C#.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creational Patterns : Factory Method</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Thu, 05 Dec 2024 17:10:33 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/creational-patterns-factory-method-1ga8</link>
      <guid>https://forem.com/silentcoder52626/creational-patterns-factory-method-1ga8</guid>
      <description>&lt;h2&gt;
  
  
  Factory Method
&lt;/h2&gt;

&lt;p&gt;The Factory Method is a creational design pattern that defines an interface for creating objects but lets subclasses alter the type of objects that will be created.&lt;/p&gt;

&lt;p&gt;It promotes loose coupling and enhances flexibility by delegating object instantiation to subclasses.&lt;/p&gt;

&lt;p&gt;This pattern is useful when a class cannot anticipate the type of objects it must create or wants to delegate creation to derived classes.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System;

&lt;p&gt;// Step 1: Define the Product interface&lt;br&gt;
public interface IAnimal&lt;br&gt;
{&lt;br&gt;
    void Speak();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Step 2: Create Concrete Products&lt;br&gt;
public class Dog : IAnimal&lt;br&gt;
{&lt;br&gt;
    public void Speak() =&amp;gt; Console.WriteLine("Woof!");&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;public class Cat : IAnimal&lt;br&gt;
{&lt;br&gt;
    public void Speak() =&amp;gt; Console.WriteLine("Meow!");&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Step 3: Create the Creator (Factory) Abstract Class&lt;br&gt;
public abstract class AnimalFactory&lt;br&gt;
{&lt;br&gt;
    public abstract IAnimal CreateAnimal();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Step 4: Implement Concrete Creators&lt;br&gt;
public class DogFactory : AnimalFactory&lt;br&gt;
{&lt;br&gt;
    public override IAnimal CreateAnimal() =&amp;gt; new Dog();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;public class CatFactory : AnimalFactory&lt;br&gt;
{&lt;br&gt;
    public override IAnimal CreateAnimal() =&amp;gt; new Cat();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Step 5: Client Code&lt;br&gt;
public class Program&lt;br&gt;
{&lt;br&gt;
    public static void Main()&lt;br&gt;
    {&lt;br&gt;
        AnimalFactory dogFactory = new DogFactory();&lt;br&gt;
        IAnimal dog = dogFactory.CreateAnimal();&lt;br&gt;
        dog.Speak(); // Output: Woof!&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    AnimalFactory catFactory = new CatFactory();
    IAnimal cat = catFactory.CreateAnimal();
    cat.Speak(); // Output: Meow!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Key Points:&lt;br&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;&lt;code&gt;IAnimal&lt;/code&gt;&lt;/strong&gt;: Defines the interface for the objects created.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Concrete Classes&lt;/strong&gt; (&lt;code&gt;Dog&lt;/code&gt;, &lt;code&gt;Cat&lt;/code&gt;): Implement the interface.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Abstract Factory (&lt;code&gt;AnimalFactory&lt;/code&gt;)&lt;/strong&gt;: Declares the method for creating objects.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Concrete Factories (&lt;code&gt;DogFactory&lt;/code&gt;, &lt;code&gt;CatFactory&lt;/code&gt;)&lt;/strong&gt;: Override the creation method to return specific objects.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;This example shows how the Factory Method allows you to encapsulate object creation, making the system scalable and maintaining the open/closed principle.&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>factorymethod</category>
    </item>
    <item>
      <title>Adapter Design Pattern</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Mon, 15 Jan 2024 08:17:05 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/adapter-design-pattern-2ef9</link>
      <guid>https://forem.com/silentcoder52626/adapter-design-pattern-2ef9</guid>
      <description>&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Allows two completely incompatiable interface to work together.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Used when the client excepts a (target) interface&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The adapter class allows the use of the available interface and the target interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any class can work together as long as the adapter solves the issue that all classes must implement every method defined by the shared interface.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphxdb1bb5sor8mdexgcu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphxdb1bb5sor8mdexgcu.png" alt="Image description" width="616" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=qG286LQM6BU&amp;amp;list=PLhkg-R66TRTV1-mDPLQ1HL7Wk9dGRBgSd" rel="noopener noreferrer"&gt;Video Source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Adapter design pattern is used to allow incompatible interfaces to work together. &lt;br&gt;
It acts as a bridge between two incompatible interfaces by converting the interface of a class into another interface that a client expects.&lt;/p&gt;

&lt;p&gt;Here's a simple example in C# to illustrate the Adapter pattern:&lt;/p&gt;

&lt;p&gt;Suppose you have an existing OldSystem class with a method Request, and you want to adapt it to work with a new system that expects a different interface, INewSystem.&lt;br&gt;
 The OldSystemAdapter class serves as the adapter to make the OldSystem compatible with the INewSystem interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System;

// Existing system with an incompatible interface
public class OldSystem
{
    public void Request()
    {
        Console.WriteLine("OldSystem is handling the request.");
    }
}

// New system with a different interface
public interface INewSystem
{
    void NewRequest();
}

// Adapter class that adapts OldSystem to INewSystem
public class OldSystemAdapter : INewSystem
{
    private readonly OldSystem oldSystem;

    public OldSystemAdapter(OldSystem oldSystem)
    {
        this.oldSystem = oldSystem;
    }

    public void NewRequest()
    {
        // Call the existing OldSystem's method from the adapted interface
        oldSystem.Request();
    }
}

// Client code that expects INewSystem interface
public class Client
{
    public void UseSystem(INewSystem system)
    {
        system.NewRequest();
    }
}

class Program
{
    static void Main()
    {
        // Using the existing OldSystem with the help of the adapter
        OldSystem oldSystem = new OldSystem();
        INewSystem adapter = new OldSystemAdapter(oldSystem);

        // Client code works with the INewSystem interface
        Client client = new Client();
        client.UseSystem(adapter);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;p&gt;OldSystem is the existing class with an incompatible interface.&lt;br&gt;
INewSystem is the new interface expected by the client.&lt;br&gt;
OldSystemAdapter is the adapter class that implements INewSystem by delegating calls to the existing OldSystem.&lt;/p&gt;

&lt;p&gt;The client code (Client class) works with INewSystem and is unaware of the existence of OldSystem.&lt;br&gt;
 The adapter bridges the gap between the existing system and the client's expectations.&lt;/p&gt;

&lt;p&gt;This is a simplified example, but it illustrates the basic structure of the Adapter pattern. In real-world scenarios, you might encounter more complex interfaces and interactions.&lt;/p&gt;

</description>
      <category>gof</category>
      <category>design</category>
      <category>pattern</category>
      <category>adapter</category>
    </item>
    <item>
      <title>"Optimizing Email Workflows: From Ethereal Testing to Production Perfection"</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Sat, 09 Dec 2023 10:52:11 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/optimizing-email-workflows-from-ethereal-testing-to-production-perfection-178k</link>
      <guid>https://forem.com/silentcoder52626/optimizing-email-workflows-from-ethereal-testing-to-production-perfection-178k</guid>
      <description>&lt;p&gt;Sending automated emails is a fundamental feature in modern software development. However, sharing the credentials of your official Google Account during the development phase might not be the wisest choice. Enter Ethereal Email, a game-changer in the world of test email servers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://ethereal.email/" rel="noopener noreferrer"&gt;Ethereal&lt;/a&gt;&lt;/strong&gt; is a faux SMTP service, designed primarily for Nodemailer and EmailEngine users, though its applications are not limited to these. This free, anti-transactional email service ensures messages never reach their intended destination, making it ideal for development.&lt;/p&gt;

&lt;p&gt;Having utilized Ethereal Email for over two years, I can attest to its flawless performance, especially in collaborative team environments. It seamlessly handles monkey testing scenarios, allowing you to use any dummy email as a receiver and view the messages sent in the Ethereal Email message section.&lt;/p&gt;

&lt;p&gt;Now, when it comes to production, the initial inclination might be to provide actual credentials for your official Gmail account. However, thanks to positive changes from Google, this is no longer necessary. A few tweaks to the settings, and you'll receive a random password to use for your project services.&lt;/p&gt;

&lt;p&gt;While crafting this post, I stumbled upon &lt;a href="https://shahedbd.medium.com/how-to-setup-gmail-smtp-in-asp-net-b79bf18afe4a" rel="noopener noreferrer"&gt;this guide&lt;/a&gt;, which presents a straightforward procedure for configuring your Google account as an email sender.&lt;/p&gt;

&lt;p&gt;In summary, I employ Ethereal Email as my go-to test mail server during development. I'd love to hear about your preferences and strategies for managing development and production email systems.&lt;/p&gt;

&lt;p&gt;That's all for now. Until next time!&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>email</category>
      <category>webdev</category>
      <category>testing</category>
    </item>
    <item>
      <title>ABSTRACT CLASS VS INTERFACE</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Thu, 31 Aug 2023 11:56:43 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/abstract-class-vs-interface-1le8</link>
      <guid>https://forem.com/silentcoder52626/abstract-class-vs-interface-1le8</guid>
      <description>&lt;p&gt;One of the most frequently asked questions in any technical interview is "Tell me something about abstract class and interface."&lt;br&gt;
Although it is rather straightforward, occasionally it can be difficult to distinguish between them.&lt;/p&gt;

&lt;p&gt;Let's begin with their description:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Abstract Class&lt;/strong&gt;&lt;br&gt;
      A class that cannot be created on its own but is frequently used as a model for other classes is referred to as an abstract class. It&lt;br&gt;
may include both concrete (implemented) and abstract (not yet&lt;br&gt;
implemented) methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interface&lt;/strong&gt;&lt;br&gt;
      An interface refers to a contract that specifies a group of methods that a class which implements it, must offer. It does not include any implementation; it simply contains method signatures.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The most important thing to take away from this is that although &lt;strong&gt;Interface only has unimplemented methods&lt;/strong&gt;, &lt;strong&gt;Abstract classes&lt;/strong&gt; can contain &lt;strong&gt;both implemented and unimplemented methods&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Abstract Class&lt;/strong&gt;&lt;br&gt;
        Use an abstract class when you have a base class with shared behavior between its subclasses. Some methods should have a standard implementation, while others should be left up to subclasses to implement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interface&lt;/strong&gt; &lt;br&gt;
       Use an interface when you wish to establish a contract that several classes can operate without imposing a particular class hierarchy. Method signatures may be inherited more than once.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Implementing a method:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Abstract Class&lt;/strong&gt;&lt;br&gt;
Both abstract and concrete methods are possible for an abstract class.&lt;br&gt;
The abstract methods must have implementations provided by subclasses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interface:&lt;/strong&gt;&lt;br&gt;
 Only method signatures are present; no implementation is&lt;br&gt;
present. All of the methods in an interface must have concrete&lt;br&gt;
implementations in classes that implement the interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;I have an simple real life example which will make it more clear.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Abstract Class&lt;/strong&gt;&lt;br&gt;
Abstract class are more like a company employee's. Company can have full time employee who will handle day to day work. And, company can also hire part time employee who will handle that specific work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interface&lt;/strong&gt;&lt;br&gt;
Interface is like a job contractor. You just define job to be done to a contractor and they will handle the rest. if any employee leaves the work than it won't matter to you because it will be handled by the contractor itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Let's explain with the help of some code examples.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
using System;
// Abstract class  
abstract  class  Shape
{ 
// Concrete method  
    public void PrintColor() 
    { 
        Console.WriteLine("The shape is colored."); 
    } 
    // Abstract method to calculate area (to be implemented by subclasses)  
    public abstract double CalculateArea(); 
} 
// Concrete subclass  
class  Circle : Shape 
{ 
    private  double radius; 
    public Circle(double radius) 
    { 
        this.radius = radius; 
    } 
    public override double CalculateArea() 
    { 
        return Math.PI * radius * radius; 
    } 
} 
// Concrete subclass 
class  Rectangle : Shape 
{ 
    private  double length;
    private  double width; 
    public Rectangle(double length, double width) 
    { 
        this.length = length; this.width = width; 
    } 
    public override double CalculateArea() 
    { 
        return length * width; 
    } 
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this C# example, the &lt;code&gt;Shape&lt;/code&gt; class is an abstract class with a concrete method &lt;code&gt;PrintColor()&lt;/code&gt; and an abstract method &lt;code&gt;CalculateArea()&lt;/code&gt;. Subclasses &lt;code&gt;Circle&lt;/code&gt; and &lt;code&gt;Rectangle&lt;/code&gt; inherit from &lt;code&gt;Shape&lt;/code&gt; and provide concrete implementations for the &lt;code&gt;CalculateArea()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System;

// Interface for drawing
interface IDrawable
{
    void Draw();
}

// Interface for filling
interface IFillable
{
    void Fill();
}

// Class implementing both interfaces
class Circle : IDrawable, IFillable
{
    private double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public void Draw()
    {
        // Implementation for drawing a circle
        Console.WriteLine("Drawing a circle.");
    }

    public void Fill()
    {
        // Implementation for filling a circle
        Console.WriteLine("Filling a circle.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this C# example, we have two interfaces, &lt;code&gt;IDrawable&lt;/code&gt; and &lt;code&gt;IFillable&lt;/code&gt;, each with a method signature. The &lt;code&gt;Circle&lt;/code&gt; class implements both interfaces and provides concrete implementations for the &lt;code&gt;Draw()&lt;/code&gt; and &lt;code&gt;Fill()&lt;/code&gt; methods. This demonstrates how C# allows a class to implement multiple interfaces to achieve multiple inheritance of method signatures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;I guess that's all. Hope you understand. Enjoy.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>abstract</category>
      <category>interface</category>
      <category>interview</category>
      <category>programming</category>
    </item>
    <item>
      <title>BOILERPLATE AND WHY?</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Fri, 25 Aug 2023 07:16:43 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/boilerplate-and-why-2go1</link>
      <guid>https://forem.com/silentcoder52626/boilerplate-and-why-2go1</guid>
      <description>&lt;h1&gt;
  
  
  Why Boilerplate Code is Important in the Development of Software
&lt;/h1&gt;

&lt;p&gt;Boilerplate code is frequently seen in the context of software development with mixed feelings. It is viewed as redundant and time-consuming on the one hand, and necessary for effective and maintained codebases on the other. The importance of boilerplate code will be discussed in this essay, along with the reasons why it should be embraced rather than avoided.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boilerplate Code: What is it?
&lt;/h2&gt;

&lt;p&gt;Code that needs to be written repeatedly with little to no variation is referred to as boilerplate code. This contains the setup code, configuration files, imports, and other repetitious building blocks that make up a software project's framework. Although it may seem like a boring process, boilerplate code has a number of important functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Regularity and Standardization
&lt;/h3&gt;

&lt;p&gt;Boilerplate code helps maintain consistency and standardization inside a codebase, which is one of its main benefits. The code is simpler to comprehend, maintain, and extend when developers adhere to a set framework for their projects. This consistency applies to all teams and projects, which improves teamwork.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Reduced Error Rates
&lt;/h3&gt;

&lt;p&gt;Best practices and necessary error-checking methods are frequently included in boilerplate code. For instance, it might consist of security protections, input validation, and exception handling. Boilerplate code is frequently used because it makes engineers less likely to forget these important details, which lowers the risk of errors and security flaws.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Faster Development
&lt;/h3&gt;

&lt;p&gt;The use of boilerplate code speeds up development. They can use pre-existing templates or code snippets to reduce the amount of time spent on development by not having to reinvent the wheel for every project. This is very helpful when setting up a build system, a project structure, or initializing a database connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Maintenance and Scalability
&lt;/h3&gt;

&lt;p&gt;Software projects frequently need upgrades, improvements, or bug patches as they develop. By enabling uniform application of changes, boilerplate code encourages maintainability. Developers can make changes with better assurance and predictability when they are aware of specific code patterns. Large-scale projects with a long lifespan require this.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Code Readability
&lt;/h3&gt;

&lt;p&gt;Maintainable code is readable code. When boilerplate code is organized correctly, readability is improved. It offers a reliable structure that programmers may adhere to. This uniformity improves the readability of the entire codebase and makes it simpler for team members to understand each other's code.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Code Reusability
&lt;/h3&gt;

&lt;p&gt;The common functionalities that can be reused across projects are frequently encapsulated in boilerplate code. Reusable boilerplate code includes frameworks, libraries, and code generators. By utilizing these tools, developers can lessen redundancy and take use of tried-and-true methods, further enhancing the caliber of their product.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embracing Boilerplate Code
&lt;/h2&gt;

&lt;p&gt;Let's talk about how to successfully use boilerplate code now that we've established its significance:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Templates and Generators
&lt;/h3&gt;

&lt;p&gt;Use code generators and templates if you can. You can construct unique templates for your boilerplate code using tools found in many contemporary integrated development environments (IDEs). You might also look at code generation tools that are tailored to your programming language or framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Code Reviews and Standards
&lt;/h3&gt;

&lt;p&gt;Make your code review process include boilerplate coding guidelines. Establish coding guidelines, including boilerplate code norms, and make sure that everyone on the team follows them. Your projects will become more consistent and maintainable as a result.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Reusable Libraries and Frameworks
&lt;/h3&gt;

&lt;p&gt;Utilize reusable frameworks and libraries that offer well-organized boilerplate code for typical tasks. For instance, web developers can use well-known frameworks with predetermined project structures and conventions.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Documentation
&lt;/h3&gt;

&lt;p&gt;Effectively document your boilerplate code. Boilerplate sections' purpose and usage should be made clear in comments and documentation so that developers can more easily comprehend them and make the necessary modifications.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Continuous Improvement
&lt;/h3&gt;

&lt;p&gt;Boilerplate code shouldn't stay the same forever. Review and update your boilerplate code frequently to include the latest best practices, innovations, and security precautions. This makes sure that it continues to be an important component of your development process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Software development that is effective has boilerplate code on its side rather than against it. Boilerplate code is a vital weapon in a developer's toolbox since it offers uniformity, lowers errors, speeds up development, and improves maintainability. Boilerplate code should be embraced rather than avoided since it serves as a springboard for creating strong, dependable, and scalable software solutions. Boilerplate code continues to provide a solid basis upon which to create unique and fruitful projects in the ever-changing world of software development.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Improve your Javascript debugging Skills</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Fri, 03 Mar 2023 15:44:58 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/improve-your-javascript-debugging-skills-50ia</link>
      <guid>https://forem.com/silentcoder52626/improve-your-javascript-debugging-skills-50ia</guid>
      <description>&lt;p&gt;Debugging a code is an art. If you don't have good debugging skills, it can really be pain in the ass. If you don't understand the working mechanism of Javascript code, finding a bug can be hard. Having knowledge of debugging tools/methods that JS provides, it can be really helpful . Besides, having good knowledge of debugging can save a lot of time. Following are some of the methods / statement in JS that aids debugging .&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;console.log&lt;/li&gt;
&lt;li&gt;console.table&lt;/li&gt;
&lt;li&gt;console.trace&lt;/li&gt;
&lt;li&gt;console.warn&lt;/li&gt;
&lt;li&gt;console.error&lt;/li&gt;
&lt;li&gt;console.assert&lt;/li&gt;
&lt;li&gt;debugger&lt;/li&gt;
&lt;li&gt;console.time &amp;amp;&amp;amp; console.timeEnd&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get into these in detail&lt;/p&gt;

&lt;h2&gt;
  
  
  console.log
&lt;/h2&gt;

&lt;p&gt;This method takes any number of arguments and prints the value in console window. It is helpful when you want to know the value of variable at specific instant of time.&lt;/p&gt;

&lt;p&gt;Usage:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let student ={
 firstName: "Niroj",
 lastName: "Dahal"
}
console.log('student detail',student);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgr4luxr23v0rwr8zuh66.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgr4luxr23v0rwr8zuh66.png" alt="Result of above code" width="697" height="192"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  console.table
&lt;/h2&gt;

&lt;p&gt;This method is useful when you want to see the values of array in console. It shows the data in table format so that the datas are readable&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let values = [1,2,3,4,5,6];

console.table(values);     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0miwi84ikzstysejfxc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0miwi84ikzstysejfxc.png" alt="Result of above code" width="691" height="282"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  console.trace
&lt;/h2&gt;

&lt;p&gt;This methods prints out the stack trace of code. It is useful when same method is called from multiple places and we are having hard time figuring out why and from where the specified method is called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function CalledMethod(){
    console.trace('trace from where this method is called');
}

function CallerMethod1(){
   CalledMethod();
}

function CallerMethod2(){
   CalledMethod();
}
CallerMethod1();
CallerMethod2();    

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8uwq6ez12cg1g5h9zcm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8uwq6ez12cg1g5h9zcm.png" alt="Result of Above Code" width="800" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we called  &lt;strong&gt;CalledMethod&lt;/strong&gt;  twice once through  &lt;strong&gt;CallerMethod1&lt;/strong&gt;  method and again through  &lt;strong&gt;CallerMethod2&lt;/strong&gt;  . As seen in screenshot, trace is written in console stating the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  console.warn
&lt;/h2&gt;

&lt;p&gt;This method shown message in yellow color (warning) .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.warn('This is a warning message');    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F48b6019vufdg6lcge0ia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F48b6019vufdg6lcge0ia.png" alt="Result of above code" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  console.error
&lt;/h2&gt;

&lt;p&gt;This method shows message in red color (error).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.error('This is an error message');    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3385i6oueyt1prqzeopl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3385i6oueyt1prqzeopl.png" alt="Result of above code" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  console.assert
&lt;/h2&gt;

&lt;p&gt;This method writes a message to the console if an expression evaluates to false. It is useful to log against unexpected/invalid conditions&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let studentDataFromApi=null;
console.assert(studentDataFromApi !=null,'Student data cannot be null');    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fostapur05i5ugusejgkq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fostapur05i5ugusejgkq.png" alt="Result of above code" width="800" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, since first expression&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;studentDataFromApi !=null&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;evaluates to false, second parameter message is logged.&lt;/p&gt;

&lt;h2&gt;
  
  
  debugger
&lt;/h2&gt;

&lt;p&gt;This method is used to set breakpoint during the program flow. If you want to see the values of variables at some point of time, consider using a debugger. The debugger statement stops the execution of JavaScript, and calls the debugger&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function TestDebugger(){
   let val =1,secondVal=2,thirdVal=3,forthVal=4;
    debugger;
}
TestDebugger();    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3fnz8a04amylspyahphw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3fnz8a04amylspyahphw.png" alt="Result of above code" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In screenshot , we can clearly see the value of variables of state before debugger is called.&lt;/p&gt;

&lt;h2&gt;
  
  
  console.time &amp;amp;&amp;amp; console.timeEnd
&lt;/h2&gt;

&lt;p&gt;These methods are used to print the execution time of the code .&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;console.time starts the timer and console.timeEnd stops previous timer and logs the time taken to execute code between those methods. Both methods take a string parameter for named timer. You can specify name to timer in console.time method and specify same parameter to stop a timer. If parameter is not provided, name defaults to  &lt;strong&gt;default&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time('start');
console.timeEnd('start');    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result Screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnb58n8pixh5aj8ji3ogp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnb58n8pixh5aj8ji3ogp.png" alt="Result of above code" width="562" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading and happy debugging!&lt;/p&gt;

</description>
      <category>productdesign</category>
      <category>designers</category>
      <category>uidesign</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Refactoring To Improve Performance</title>
      <dc:creator>Common Khadka</dc:creator>
      <pubDate>Fri, 03 Mar 2023 15:08:27 +0000</pubDate>
      <link>https://forem.com/silentcoder52626/refactoring-to-improve-performance-5epf</link>
      <guid>https://forem.com/silentcoder52626/refactoring-to-improve-performance-5epf</guid>
      <description>&lt;p&gt;We, as developers, get lost in getting a feature complete within a time-frame and negate performance most of the time. Currently, I am working on an application where I was requested to improve performance. A page was taking around 14-15s to get loaded. I brought it down to 4-5s although it is still a lot to wait. Here are some things that impacted performance of application and how I sorted it out&lt;/p&gt;

&lt;h2&gt;
  
  
  Lazy-loading everywhere
&lt;/h2&gt;

&lt;p&gt;We used Entity Framework as ORM. In order to process data, we had couple of foreach statements and nested loops . Inside loop, properties of referenced entities and collections were manipulated. Loading such data's eagerly reduced around 100 queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automapper used everywhere
&lt;/h2&gt;

&lt;p&gt;Automapper was used extensively in our application. Suppose we had an Address Entity and it had reference to State entity which further had reference to Province entity , we mapped Address entity to AddressViewModel using automapper. Even when I needed detail of Address entity in AddressViewModel, it would map all details of references in AddressViewModel. One way to get rid of it was to configure Automapper in a way to exclude mapping references of entity but that would cause our application to break since it was used elsewhere. Mapping only required references helped to improve performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting every fields from Database
&lt;/h2&gt;

&lt;p&gt;Even if only some fields in a table were needed, every fields were retrieved from database. Some of the columns stored large amount of data's which resulted in weak performance. Selecting only the required fields improved performance by some margin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Less Use of TryParse
&lt;/h2&gt;

&lt;p&gt;In order to parse Enum from string or int , code used to wrapped inside Try/Catch block . If exception was caught, do something. Exceptions are meant to be unintentional but we depend on exceptions to perform an action. Changing those statements to use TryParse improved readability and performance slightly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unaware of SelectMany querying pattern
&lt;/h2&gt;

&lt;p&gt;Whenever we needed a reference inside collections , we did SelectMany . Our common misconception was SelectMany does a single query to database and retrieved data. Using a profiler showed more number of queries than expected. Suppose , we had 5 items in collection and we used SelectMany to get references inside a collection, it did 5 queries where only 1 was expected. At the end, our solution was to issue a single query to required reference table.&lt;/p&gt;

&lt;p&gt;That's all for now. &lt;strong&gt;Happy Coding !!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>hiring</category>
      <category>privacy</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
