<?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: Nawaf Mahsoun</title>
    <description>The latest articles on DEV Community by Nawaf Mahsoun (@nawafmahsoun).</description>
    <link>https://dev.to/nawafmahsoun</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%2F1094462%2F175e84dd-f95e-4cc8-bf43-4c64d9ce9869.jpeg</url>
      <title>DEV Community: Nawaf Mahsoun</title>
      <link>https://dev.to/nawafmahsoun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nawafmahsoun"/>
    <language>en</language>
    <item>
      <title>🚀 Exploring the Power of init and private set in .NET Development 💎🔒</title>
      <dc:creator>Nawaf Mahsoun</dc:creator>
      <pubDate>Sat, 27 Sep 2025 09:32:16 +0000</pubDate>
      <link>https://dev.to/nawafmahsoun/exploring-the-power-of-init-and-private-set-in-net-development-2pmj</link>
      <guid>https://dev.to/nawafmahsoun/exploring-the-power-of-init-and-private-set-in-net-development-2pmj</guid>
      <description>&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%2Fyqb2bp0hqncv4umwoqru.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%2Fyqb2bp0hqncv4umwoqru.png" alt=" " width="800" height="737"&gt;&lt;/a&gt;In my .&lt;strong&gt;NET&lt;/strong&gt; journey, I’ve explored the subtle yet impactful world of property &lt;strong&gt;setters&lt;/strong&gt;, where the choice between &lt;strong&gt;init&lt;/strong&gt; and &lt;strong&gt;private set&lt;/strong&gt; can make your &lt;strong&gt;domain models&lt;/strong&gt; safer and your &lt;strong&gt;code&lt;/strong&gt; cleaner. These concepts &lt;strong&gt;highlight&lt;/strong&gt; the balance between immutability and controlled flexibility.&lt;/p&gt;

&lt;p&gt;🔍 &lt;strong&gt;Understanding the Basics&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;init&lt;/strong&gt; 💎 allows setting a property only during initialization (&lt;strong&gt;constructor&lt;/strong&gt; or &lt;strong&gt;object initializer&lt;/strong&gt;). After that, it’s &lt;strong&gt;read-only.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;private set **🔒 allows the property to change, but only within the **class&lt;/strong&gt;, not from outside.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why does it matter?&lt;/strong&gt;&lt;br&gt;
Using the right setter pattern prevents accidental mutations and enforces your design intent — crucial for building robust and &lt;strong&gt;maintainable&lt;/strong&gt; applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🎯 Let’s Illustrate with an Example:&lt;/strong&gt;&lt;br&gt;
`&lt;br&gt;
public class Asset&lt;br&gt;
{&lt;br&gt;
    public Guid Id { get; init; }&lt;br&gt;
    public DateTime CreatedAt { get; init; }&lt;br&gt;
    public string? CreatedBy { get; init; }&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public DateTime? UpdatedAt { get; private set; }
public string? UpdatedBy { get; private set; }

public void MarkUpdated(string user)
{
    UpdatedAt = DateTime.UtcNow;
    UpdatedBy = user;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;💡 Expert Insight:&lt;/p&gt;

&lt;p&gt;Use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for values that should never change after creation (Id, CreatedAt).&lt;/p&gt;

&lt;p&gt;Use private &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/..." 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/..." alt="Uploading image" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;domain methods for values that evolve (UpdatedAt, Status).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;EF Core still works seamlessly with init properties during materialization thanks to reflection.&lt;/p&gt;

&lt;p&gt;🤔 Share Your Experience:&lt;br&gt;
How do you handle immutability vs flexibility in your .NET domain models? Any tips or patterns that improved your design? Join the conversation below! 👇&lt;/p&gt;

&lt;h1&gt;
  
  
  DotNET #CSharp #SoftwareDevelopment #CleanArchitecture #BestPractices #CodingTips
&lt;/h1&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🚀 Unleash the Power of Delegates in C# 🚀</title>
      <dc:creator>Nawaf Mahsoun</dc:creator>
      <pubDate>Tue, 29 Apr 2025 17:10:00 +0000</pubDate>
      <link>https://dev.to/nawafmahsoun/unleash-the-power-of-delegates-in-c-8hb</link>
      <guid>https://dev.to/nawafmahsoun/unleash-the-power-of-delegates-in-c-8hb</guid>
      <description>&lt;p&gt;Delegates act as “remote controls” for methods in C#, letting you treat behavior as a first-class object. Pass them around, invoke them anywhere, and swap implementations at runtime—without tightly coupling your components.&lt;/p&gt;




&lt;p&gt;🔧 &lt;strong&gt;Decoupling Components&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By depending on a delegate instead of calling a specific method directly, your code becomes far more flexible and testable. You can inject custom logic, swap out implementations, or mock callbacks—all without touching the original source.&lt;br&gt;
🎯 &lt;strong&gt;Simplifying Events &amp;amp; Callbacks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Events in C# are built on multicast delegates—subscribe, unsubscribe, and broadcast with ease.
-Callbacks empower consumers to decide “what happens next,” ideal for asynchronous workflows, progress reporting, and more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡** Built-In Delegate Shortcuts**&lt;/p&gt;

&lt;p&gt;No need to define your own delegate types every time. The BCL gives you:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`Action&amp;lt;T…&amp;gt;` for methods that return `void`

`Func&amp;lt;T…, TResult&amp;gt;` for methods that return a value

`Predicate&amp;lt;T&amp;gt;` for methods that return a `bool`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Action&amp;lt;string&amp;gt; log       = msg =&amp;gt; Console.WriteLine($"📢 {msg}");
Func&amp;lt;int, int, int&amp;gt; add  = (a, b) =&amp;gt; a + b;
Predicate&amp;lt;int&amp;gt; isEven    = n =&amp;gt; n % 2 == 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;🛠️ Real-World Example&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;// 1) Define a custom delegate
public delegate void IntAction(int x);

// 2) Bind it to any matching method or lambda
IntAction printer  = x =&amp;gt; Console.WriteLine($"Number: {x}");
IntAction doubler  = x =&amp;gt; Console.WriteLine($"Double: {x * 2}");

// 3) Compose multiple methods
printer += doubler;

// 4) Invoke all subscribers
printer(5);

// ▶️ Output:
// Number: 5
// Double: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Key Takeaway:&lt;/strong&gt;&lt;br&gt;
Leveraging delegates transforms your C# code into a more expressive, extensible, and maintainable design. Master them, and watch your software architectures evolve! 💪✨&lt;/p&gt;

&lt;h1&gt;
  
  
  CSharp #DotNet #CleanCode #Delegates #SoftwareCraftsmanship
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>When a Tiny Bug in C# Breaks Everything: London vs. Classical Testing</title>
      <dc:creator>Nawaf Mahsoun</dc:creator>
      <pubDate>Tue, 22 Apr 2025 15:15:21 +0000</pubDate>
      <link>https://dev.to/nawafmahsoun/when-a-tiny-bug-in-c-breaks-everything-london-vs-classical-testing-ae2</link>
      <guid>https://dev.to/nawafmahsoun/when-a-tiny-bug-in-c-breaks-everything-london-vs-classical-testing-ae2</guid>
      <description>&lt;p&gt;Ever had a tiny bug in your C# code crash your entire test suite? 😩 Let’s break down why that happens—and how your testing style (London vs. Classical) changes your debugging experience.&lt;/p&gt;

&lt;p&gt;Inspired by “Unit Testing: Principles, Practices, and Patterns” by Vladimir Khorikov, here’s a relatable C# example to highlight the difference!&lt;/p&gt;




&lt;p&gt;🛒 &lt;strong&gt;The Scenario: A Simple Shopping Cart System&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine an e-commerce setup with:    &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Cart&lt;/code&gt;– Manages items, calculates totals&lt;/li&gt;
&lt;li&gt;    &lt;code&gt;OrderProcessor&lt;/code&gt;– Handles orders (depends on &lt;code&gt;Cart&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;    &lt;code&gt;PaymentService&lt;/code&gt;– Processes payments (depends on OrderProcessor)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now suppose you accidentally break the &lt;code&gt;Cart&lt;/code&gt;class. How does each testing style react?&lt;/p&gt;




&lt;p&gt;1️⃣ &lt;strong&gt;London-Style Tests (Isolated with Mocks)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In London-style testing, dependencies are mocked, so only tests for the broken component (&lt;code&gt;Cart&lt;/code&gt;) fail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// London-style test for OrderProcessor (using Moq)
[Fact]
public void OrderProcessor_CreatesInvoice_ReturnsCorrectTotal()
{
    var mockCart = new Mock&amp;lt;ICart&amp;gt;();
    mockCart.Setup(c =&amp;gt; c.GetTotal()).Returns(100); // Mocked Cart
    var processor = new OrderProcessor(mockCart.Object);

    var result = processor.CreateInvoice();    
    Assert.Equal(100, result); // ✅ Passes—even if the REAL Cart is broken!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Pros&lt;/strong&gt;: Fast, focused tests. Easy to pinpoint failures (e.g., Cart only).&lt;br&gt;
❌ &lt;strong&gt;Cons&lt;/strong&gt;: Can give false confidence—misses integration bugs.&lt;/p&gt;




&lt;p&gt;In classical testing, real dependencies are used. So if &lt;code&gt;Cart&lt;/code&gt;is broken, tests for &lt;code&gt;OrderProcessor&lt;/code&gt;and even &lt;code&gt;PaymentService&lt;/code&gt;start failing too.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Classical test for OrderProcessor
[Fact]
public void OrderProcessor_CreatesInvoice_ReturnsCorrectTotal()
{
    var cart = new Cart(); // Real Cart instance
    cart.AddItem("Book", 50);
    var processor = new OrderProcessor(cart);

    var result = processor.CreateInvoice();    
    Assert.Equal(50, result); // ❌ FAILS if Cart.GetTotal() is broken!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Pros&lt;/strong&gt;: Detects integration issues early.&lt;br&gt;
❌ &lt;strong&gt;Cons&lt;/strong&gt;: One bug can trigger a failure avalanche 🌊&lt;/p&gt;




&lt;p&gt;🧠 &lt;strong&gt;Why This Matters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;London-style tests are precise and fast—but can miss real-world breakages.&lt;br&gt;
Classical tests offer stronger guarantees—but debugging them can feel like “whack-a-mole.” 🔨&lt;/p&gt;




&lt;p&gt;📘 &lt;strong&gt;Khorikov’s Insight&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Classical tests align better with the Test Pyramid, emphasizing integration and reducing over-reliance on mocks.&lt;/li&gt;
&lt;li&gt;    A failure avalanche often points to mission-critical code. If breaking &lt;code&gt;Cart&lt;/code&gt;causes 50 failures? That’s a signal—it’s important!&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;✅ &lt;strong&gt;Silver Linings&lt;/strong&gt;    &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run tests frequently: If things break, you’ll know you broke it 😅&lt;/li&gt;
&lt;li&gt;    Volume of failure = Importance: Failures are feedback, not frustration&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;💬 &lt;strong&gt;Final Thought&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Which testing style do you lean toward?&lt;/p&gt;

&lt;p&gt;🔍 *&lt;em&gt;London *&lt;/em&gt;– for precision and speed?&lt;br&gt;
🌊 *&lt;em&gt;Classical *&lt;/em&gt;– for real-world system feedback?&lt;/p&gt;

&lt;p&gt;Let’s discuss below—and if you haven’t read Khorikov’s book yet, seriously, it’s a game-changer. 🎯&lt;/p&gt;




&lt;h1&gt;
  
  
  SoftwareEngineering #Testing #DotNet #CSharp #CleanCode #TDD
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>Understanding Boxing and Unboxing in .NET 🥊📦</title>
      <dc:creator>Nawaf Mahsoun</dc:creator>
      <pubDate>Mon, 31 Mar 2025 16:33:00 +0000</pubDate>
      <link>https://dev.to/nawafmahsoun/boxing-vs-unboxing-a-lesson-in-performance-3j3j</link>
      <guid>https://dev.to/nawafmahsoun/boxing-vs-unboxing-a-lesson-in-performance-3j3j</guid>
      <description>&lt;p&gt;As a .NET developer, I’ve come to appreciate how small coding decisions can ripple into significant performance impacts. Today, let’s unpack boxing and unboxing—subtle operations that balance convenience with efficiency.&lt;br&gt;
&lt;strong&gt;What are Boxing and Unboxing?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Boxing&lt;/strong&gt;&lt;br&gt;
Boxing is the process of converting a value type (such as an &lt;code&gt;int&lt;/code&gt;or &lt;code&gt;struct&lt;/code&gt;) into a reference type (&lt;code&gt;object&lt;/code&gt;). This operation involves:&lt;/p&gt;

&lt;p&gt;1.Heap Allocation: Allocates memory on the heap.&lt;br&gt;
2.Data Copy: Copies the value type’s data from the stack to the heap.&lt;br&gt;
3.Reference Creation: Creates a reference to the new heap-allocated &lt;br&gt;
object..&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unboxing&lt;/strong&gt;&lt;br&gt;
Unboxing is the reverse process—extracting the value type from the heap-stored object back to the stack. Key points include:&lt;/p&gt;

&lt;p&gt;1.Explicit Casting: An explicit cast is required, for example &lt;code&gt;(int)boxedNumber&lt;/code&gt;.&lt;br&gt;
2.Type Safety: Incorrect casts result in an &lt;code&gt;InvalidCastException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Should You Care?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Boxing and unboxing introduce several performance overheads:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;.Memory Pressure: Heap allocations increase garbage collection workload.&lt;/li&gt;
&lt;li&gt;.CPU Cost: Copying data between the stack and the heap consumes extra CPU cycles.&lt;/li&gt;
&lt;li&gt;.Type Safety Risks: Unboxing mandates precise casting, increasing the risk of runtime exceptions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code Example&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;// Value type on the stack  
int number = 42;  

// Boxing: converting int to object (heap allocation)
object boxedNumber = number;  

// Unboxing: extracting int from the object (requires explicit cast)
int unboxedNumber = (int)boxedNumber;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Danger Zone&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;// Incorrect cast: throws InvalidCastException!
double invalidUnbox = (double)boxedNumber;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Best Practices to Avoid Unnecessary Boxing&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prefer Generics Over Non-Generic Collections
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // Using ArrayList causes boxing
 ArrayList list = new ArrayList();
  list.Add(42); // Boxing happens here!

 // Using List&amp;lt;T&amp;gt; avoids boxing
 List&amp;lt;int&amp;gt; genericList = new List&amp;lt;int&amp;gt;();
 genericList.Add(42);  // No boxing occurs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Avoid Object Parameters
Avoid passing value types as &lt;code&gt;object&lt;/code&gt;in performance-critical code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3.Be Cautious with Interfaces&lt;br&gt;
Casting a value type to an interface (e.g., &lt;code&gt;IFormattable&lt;/code&gt;) will trigger boxing.&lt;br&gt;
&lt;strong&gt;Performance Impact&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a loop of 10 million iterations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Without boxing: ~50 ms&lt;/li&gt;
&lt;li&gt;With boxing: ~500 ms (10x slower!)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tools like &lt;a href="https://github.com/dotnet/BenchmarkDotNet" rel="noopener noreferrer"&gt;BenchmarkDotNet &lt;/a&gt;or the IL Disassembler can help identify hidden boxing in your code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Have you encountered unexpected boxing/unboxing issues?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What strategies do you use to minimize heap allocations?&lt;br&gt;
Any war stories or optimization tricks? Share below! 👇&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>codingtips</category>
    </item>
  </channel>
</rss>
