<?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: Hamid Molareza</title>
    <description>The latest articles on DEV Community by Hamid Molareza (@hamidmolareza).</description>
    <link>https://dev.to/hamidmolareza</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%2F1247350%2Fa390e6b7-324d-4ccb-ad99-56ee1304ce15.png</url>
      <title>DEV Community: Hamid Molareza</title>
      <link>https://dev.to/hamidmolareza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamidmolareza"/>
    <language>en</language>
    <item>
      <title>🍳 Asynchronous Programming in C# Made Easy: Breakfast Included! ☕️</title>
      <dc:creator>Hamid Molareza</dc:creator>
      <pubDate>Tue, 16 Jan 2024 10:28:19 +0000</pubDate>
      <link>https://dev.to/hamidmolareza/asynchronous-programming-in-c-made-easy-breakfast-included-3bcj</link>
      <guid>https://dev.to/hamidmolareza/asynchronous-programming-in-c-made-easy-breakfast-included-3bcj</guid>
      <description>&lt;p&gt;Hey .NET devs! 👋&lt;/p&gt;

&lt;p&gt;Want to make your C# code smoother and more efficient? Asynchronous programming is the key! This awesome article from Microsoft dives deep into everything you need to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is asynchronous programming and why is it important?&lt;/li&gt;
&lt;li&gt;Unlock the benefits of asynchronous coding.&lt;/li&gt;
&lt;li&gt;Master the async and await keywords like a pro.&lt;/li&gt;
&lt;li&gt;Whip up a delicious asynchronous breakfast (seriously!).&lt;/li&gt;
&lt;li&gt;Discover next steps to level up your async skills.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ready to take your C# game to the next level?&lt;br&gt;
&lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/"&gt;Read the article now&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>asynchronous</category>
      <category>programming</category>
    </item>
    <item>
      <title>Demystifying OOP in C#: Your Guide to Building Robust and Flexible Applications</title>
      <dc:creator>Hamid Molareza</dc:creator>
      <pubDate>Wed, 10 Jan 2024 18:01:01 +0000</pubDate>
      <link>https://dev.to/hamidmolareza/demystifying-oop-in-c-your-guide-to-building-robust-and-flexible-applications-1ncf</link>
      <guid>https://dev.to/hamidmolareza/demystifying-oop-in-c-your-guide-to-building-robust-and-flexible-applications-1ncf</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Prerequisite:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Familiarity with &lt;strong&gt;methods&lt;/strong&gt; in C#&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Welcome, fellow adventurers on the coding path! Today, we'll delve into the intriguing world of Object-Oriented Programming (OOP) in C#. Buckle up as we explore its fundamental concepts, unravel its advantages, and empower you to build robust and flexible applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OOP: A Mindset, Not Just Syntax&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Object orientation is so pivotal that some believe it should be the foundation of programming education. OOP isn't merely syntax; it's a way of thinking about code. While modern C# allows non-OOP approaches, object orientation remains a cornerstone in many languages, especially C#.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building Blocks of Code: Objects and Classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine coding with building blocks, each holding data and actions that snap together to create intricate structures. Each block is an object, and OOP is the blueprint for constructing and connecting them. Data becomes &lt;code&gt;properties&lt;/code&gt;, and actions become &lt;code&gt;methods&lt;/code&gt;, all bundled within classes. This organized approach simplifies complex programs, making them easier to maintain and understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modeling the World with Objects: A Car Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The world around us is full of objects. Let's take a car, for instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Designing the Car Blueprint:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Properties:&lt;/strong&gt; A car has attributes like &lt;code&gt;color&lt;/code&gt;, &lt;code&gt;speed&lt;/code&gt;, etc., represented as properties in C#.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods:&lt;/strong&gt; A car exhibits behaviors like accelerating or braking. These become methods in C#.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementing the Car Class:&lt;/strong&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Properties&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Methods&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Accelerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Brake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&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;&lt;strong&gt;From Blueprint to Reality: Creating Instances&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just like a real car, we need to create instances of our Car class to bring it to life:&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;myCar&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;Car&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Using the `new` keyword to create instances&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;yourCar&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;Car&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Customizing with Constructors:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What if we want cars with different colors?&lt;br&gt;
Constructors come to the rescue!&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Other code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can create cars with specific colors:&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;myCar&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;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Red"&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;yourCar&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;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Blue"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Advantages of OOP:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;OOP offers compelling benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code Reusability:&lt;/strong&gt; Say goodbye to repetitive code! Inheritance allows you to create new classes that inherit properties and methods from existing ones, saving you time and effort. Think of building bigger structures by attaching pre-assembled modules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modular Design:&lt;/strong&gt; Break down your program into smaller, independent modules (classes) with well-defined interfaces. This makes your code cleaner, easier to debug, and maintainable even by others. Imagine compartmentalizing your lego creation so each section is manageable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility and Scalability:&lt;/strong&gt; Need to add a new feature? OOP makes it a breeze! Simply extend your existing classes or create new ones, without messing with the core functionality. Your lego masterpiece can accommodate new wings or towers without crumbling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;OOP: Embracing the Mindset&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think in terms of objects and their interactions, and your code will naturally flow. Remember, practice makes perfect! Start small, experiment with different classes and methods, and embrace the modularity and flexibility OOP offers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ready to Explore Further?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are some resources to fuel your coding journey:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/"&gt;Microsoft C# Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/cs/cs_oop.php"&gt;C# OOP Fundamentals&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to share your questions, thoughts, and coding triumphs in the comments below! Let's build a vibrant community of C# enthusiasts, demystifying OOP one line of code at a time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This text has been rewritten by AI.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>oop</category>
      <category>programming</category>
    </item>
    <item>
      <title>Sealed: Preventing Inheritance in C#</title>
      <dc:creator>Hamid Molareza</dc:creator>
      <pubDate>Sun, 07 Jan 2024 22:39:38 +0000</pubDate>
      <link>https://dev.to/hamidmolareza/-sealed-preventing-inheritance-in-c-3cl3</link>
      <guid>https://dev.to/hamidmolareza/-sealed-preventing-inheritance-in-c-3cl3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;By the end of this article, you can expect to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Remember and Understand:&lt;/strong&gt; Get a clear idea of what the "sealed" keyword is and what it does in C#.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Know Where and How to Use It:&lt;/strong&gt; Learn the practical aspects of when and how to use this "sealed" keyword in your C# code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the world of object-oriented programming, inheritance is a powerful technique that allows you to create new classes based on existing ones. This can be incredibly useful for code reuse and modularity, but it also introduces the potential for unexpected behavior if not handled carefully. To address this, C# provides the &lt;code&gt;sealed&lt;/code&gt; keyword, which serves as a protective barrier, preventing inheritance from certain classes or class members.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining "Sealed"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The "sealed" keyword is a modifier that restricts the ability of other classes to inherit from a specified class. Once a class is declared as sealed, it becomes a final class, unable to be extended any further. This ensures that the class's structure and behavior remain intact, preventing potential inconsistencies or unexpected changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use "Sealed"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are several reasons why you might want to use sealed classes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Encapsulation and Code Protection:&lt;/strong&gt; Sealing a class can help to protect its internal implementation and prevent unauthorized modifications. This can be particularly important for classes that handle sensitive data or perform critical operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security Considerations:&lt;/strong&gt; Sealing classes can be a valuable tool in security-critical applications. By preventing inheritance, you can reduce the risk of unexpected behavior or malicious code injection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Controlling Class Structure:&lt;/strong&gt; Sealing can be used to define a class hierarchy that is more rigid and predictable. This can be useful for ensuring that classes are used in the intended manner and that their behavior is consistent.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use Sealed Classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sealed classes are typically used when you want to create a class that should not be modified or extended by other classes. This could include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Utility Classes:&lt;/strong&gt; Classes that provide essential functionality or utilities are often sealed to prevent their behavior from being inadvertently altered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Base Classes with Fixed Behavior:&lt;/strong&gt; Classes that define a core set of functionality and should not be customized are often sealed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance-Critical Classes:&lt;/strong&gt; Sealing can improve performance by reducing the overhead of dynamic dispatch, especially in cases where a class is frequently instantiated or used.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The performance benefits of sealed classes are often minor and depend on specific code patterns and compiler decisions. (See &lt;a href="https://josipmisko.com/posts/c-sharp-sealed-class"&gt;this link&lt;/a&gt; for more information)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Core System Components:&lt;/strong&gt; Classes that form the foundation of a software application, such as fundamental data types or operating system interfaces, should be sealed to prevent unexpected or incompatible behavior from derived classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Custom Data Structures:&lt;/strong&gt; Custom data structures, such as linked lists or trees, should be sealed to prevent their underlying implementation from being altered or misused by derived classes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of 'Sealed' Class&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider a class representing a &lt;code&gt;Shape&lt;/code&gt; with properties like &lt;code&gt;color&lt;/code&gt; and &lt;code&gt;area&lt;/code&gt;. This abstract class can have sealed derived classes for specific shapes like &lt;code&gt;Circle&lt;/code&gt;, &lt;code&gt;Rectangle&lt;/code&gt;, and &lt;code&gt;Triangle&lt;/code&gt;. Each derived class can provide its unique implementation of calculating the area.&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;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;CalculateArea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;CalculateArea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PI&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&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;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;CalculateArea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&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;blockquote&gt;
&lt;p&gt;Primary Constructor is used in this code. Are you not familiar? Read &lt;a href="https://dev.to/hamidmolareza/primary-constructor-in-c-12-streamlining-object-creation-9lh"&gt;this article&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The "sealed" keyword is a valuable tool for controlling class inheritance and ensuring code integrity in C#. By understanding its purpose and using it judiciously, you can create more robust and maintainable object-oriented applications.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AI has also been used to collect and rewrite this content.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>sealed</category>
      <category>programming</category>
    </item>
    <item>
      <title>Primary Constructor in C# 12: Streamlining Object Creation</title>
      <dc:creator>Hamid Molareza</dc:creator>
      <pubDate>Sun, 07 Jan 2024 15:40:47 +0000</pubDate>
      <link>https://dev.to/hamidmolareza/primary-constructor-in-c-12-streamlining-object-creation-9lh</link>
      <guid>https://dev.to/hamidmolareza/primary-constructor-in-c-12-streamlining-object-creation-9lh</guid>
      <description>&lt;p&gt;In the world of C#, classes and structs are fundamental building blocks for creating custom data types. While these constructs already offer a mechanism for initializing objects, C# 12 introduced a new feature called &lt;code&gt;primary constructors&lt;/code&gt; that simplifies and streamlines the process of creating and initializing objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Primary Constructors&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Primary constructors are essentially a more concise way of defining constructors that directly initialize the object's properties. With primary constructors, you can define the constructor parameters within the class declaration itself, making the syntax more intuitive and readable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Declaring Primary Constructors&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To declare a primary constructor, simply place the constructor parameters within parentheses immediately after the class or struct name. For example, consider a class for representing points on a coordinate plane:&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&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;In this example, the &lt;code&gt;Point&lt;/code&gt; class has a primary constructor that takes two integer parameters, &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, representing the coordinates of the point. These parameters are directly accessible within the class body using their names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Primary Constructor Parameters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you've declared a primary constructor, you can use its parameters to initialize the object's properties. For instance, to create a point with coordinates &lt;code&gt;(10, 20)&lt;/code&gt;, you can simply instantiate the class:&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;point&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;Point&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="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code creates a new &lt;code&gt;Point&lt;/code&gt; object and assigns the values &lt;code&gt;10&lt;/code&gt; to its &lt;code&gt;x&lt;/code&gt; property and &lt;code&gt;20&lt;/code&gt; to its &lt;code&gt;y&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Primary Constructors&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Primary constructors offer several advantages over traditional constructor declarations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplified Syntax:&lt;/strong&gt; Primary constructors reduce boilerplate code and make the constructor declaration more concise.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier Readability:&lt;/strong&gt; The syntax of primary constructors makes the code more readable and easier to understand, especially for beginners.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved Maintainability:&lt;/strong&gt; Primary constructors promote code maintainability by reducing the need for repetitive code snippets.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Primary Constructors for Dependency Injection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Primary constructors can be particularly useful in dependency injection (DI) scenarios. DI is a design pattern that allows you to decouple the creation of objects from their usage, making your code more flexible and testable. With primary constructors, you can inject dependencies directly into the constructor parameters, making it easier to manage object dependencies.&lt;/p&gt;

&lt;p&gt;Consider a class &lt;code&gt;Person&lt;/code&gt; that requires a &lt;code&gt;Logger&lt;/code&gt; object to log messages:&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&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="n"&gt;logger&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;&lt;strong&gt;Primary Constructors for Inheritance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Primary constructors also make it easier to define and use inheritance hierarchies. Inheriting from a class with a primary constructor allows you to initialize the inherited properties from the base class constructor, simplifying the initialization process.&lt;/p&gt;

&lt;p&gt;Consider a &lt;code&gt;Point&lt;/code&gt; class representing points on a two-dimensional plane and a &lt;code&gt;Point3D&lt;/code&gt; class representing points on a three-dimensional plane:&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&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="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Y&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point3D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Z&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&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="n"&gt;z&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;The &lt;code&gt;Point3D&lt;/code&gt; class inherits from &lt;code&gt;Point&lt;/code&gt; and initializes its &lt;code&gt;X&lt;/code&gt; and &lt;code&gt;Y&lt;/code&gt; properties using the base class constructor. It also declares an additional property, &lt;code&gt;Z&lt;/code&gt;, to represent the third dimension.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Primary constructors are a welcome addition to C# 12, providing a more straightforward and concise way to define constructors and initialize object properties. By simplifying the constructor declaration, primary constructors make C# code more readable, maintainable, and beginner-friendly.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding Abstract Classes and Interfaces in C#</title>
      <dc:creator>Hamid Molareza</dc:creator>
      <pubDate>Thu, 04 Jan 2024 17:50:27 +0000</pubDate>
      <link>https://dev.to/hamidmolareza/understanding-abstract-classes-and-interfaces-in-c-dhk</link>
      <guid>https://dev.to/hamidmolareza/understanding-abstract-classes-and-interfaces-in-c-dhk</guid>
      <description>&lt;p&gt;&lt;strong&gt;Abstraction in C#: Demystifying the Abstract and Interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the world of object-oriented programming, C# offers two powerful tools for designing and organizing classes: abstract classes and interfaces. While they share some similarities, these two concepts serve distinct purposes and play different roles in software development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Abstract in C#?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The term "abstract" in C# refers to an entity that provides a template or blueprint for other classes to inherit from. It's like a skeletal structure that defines the common behavior and characteristics of a group of classes while leaving room for specific implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Use Abstract?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To declare an abstract class, use the &lt;code&gt;abstract&lt;/code&gt; keyword before the class name:&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;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="c1"&gt;// Abstract class&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SpeedUp&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SlowDown&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To implement an abstract class, use the &lt;code&gt;:&lt;/code&gt; symbol followed by the abstract class name:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Vehicle&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Move&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;"Engine Starts and Car Moves"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SpeedUp&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;"Pushing the accelerator, car speeds up"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SlowDown&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;"Decreasing the accelerator, car slows down"&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;Abstract methods cannot be directly instantiated, but they must be implemented by subclasses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use Abstract?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Abstract classes are particularly useful when you want to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enforce Common Behavior:&lt;/strong&gt; Define shared methods and properties that subclasses must implement, ensuring consistency across related classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Promote Code Reusability:&lt;/strong&gt; Reuse common code and functionality across different classes by inheriting from an abstract class, avoiding code duplication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define Hierarchies:&lt;/strong&gt; Represent relationships between classes, allowing inheritance and polymorphism to create a class hierarchy.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What is Interface in C#?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An interface in C# defines a set of public members, typically methods and properties, that any class implementing the interface must provide. It's like a contract that specifies the minimum functionality a class must offer.**&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use interface?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Define the Interface:&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;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ILogger&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;LogMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&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;Implement the Interface in a Class:&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConsoleLogger&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;LogMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&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;$"Logging to console: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;message&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;&lt;strong&gt;What's the Difference Between Abstract and Interface?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a table summarizing the key differences between abstract classes and interfaces:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Abstract Class&lt;/th&gt;
&lt;th&gt;Interface&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Purpose&lt;/td&gt;
&lt;td&gt;Defines a template for classes to inherit from&lt;/td&gt;
&lt;td&gt;Defines a contract for classes to implement&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implementation&lt;/td&gt;
&lt;td&gt;Can have implemented methods&lt;/td&gt;
&lt;td&gt;Cannot have implemented methods&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple Inheritance&lt;/td&gt;
&lt;td&gt;Can only inherit from one abstract class&lt;/td&gt;
&lt;td&gt;Can implement multiple interfaces&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Usage&lt;/td&gt;
&lt;td&gt;Used for defining shared behavior and functionality&lt;/td&gt;
&lt;td&gt;Used for defining the minimum required functionality&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;When Should We Use Which?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use an abstract class when:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sharing common behavior across a group of related classes&lt;/li&gt;
&lt;li&gt;Defining a class hierarchy with inheritance&lt;/li&gt;
&lt;li&gt;Enforcing a consistent base for subclasses&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use an interface when:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Specifying the minimum functionality required for different classes&lt;/li&gt;
&lt;li&gt;Promoting code reusability without inheritance&lt;/li&gt;
&lt;li&gt;Defining different ways to achieve the same task&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
Now that you've grasped the basics of abstract classes and interfaces in C#, feel free to experiment with them in your projects. If you have any questions or insights, don't hesitate to interact – like, comment, and share your thoughts!&lt;br&gt;
Happy coding!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
    </item>
    <item>
      <title>Decoding the Protected Internal Access Modifier in C#: A Balance of Encapsulation and Collaboration</title>
      <dc:creator>Hamid Molareza</dc:creator>
      <pubDate>Wed, 03 Jan 2024 09:18:49 +0000</pubDate>
      <link>https://dev.to/hamidmolareza/decoding-the-protected-internal-access-modifier-in-c-a-balance-of-encapsulation-and-collaboration-49e3</link>
      <guid>https://dev.to/hamidmolareza/decoding-the-protected-internal-access-modifier-in-c-a-balance-of-encapsulation-and-collaboration-49e3</guid>
      <description>&lt;p&gt;In the realm of programming, access modifiers play a pivotal role in regulating the visibility of class members, including methods, properties, and fields. These modifiers dictate who can access these members, ensuring &lt;strong&gt;data encapsulation and controlled information sharing&lt;/strong&gt;. Among this set of access modifiers, the &lt;strong&gt;'protected internal'&lt;/strong&gt; modifier stands apart, offering a unique blend of accessibility, striking a delicate balance between encapsulation and collaboration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delving into Protected, Internal, and Protected Internal Modifiers&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protected:&lt;/strong&gt; This modifier grants access to class members within the same class and &lt;strong&gt;derived classes&lt;/strong&gt;, regardless of the assembly to which they belong. It's ideal for sharing implementation details within a class hierarchy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Consider the 'Animal' class with a protected method 'MakeSound'. This method can be called within the 'Animal' class and in any derived classes, such as 'Dog' and 'Cat'.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&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;MakeSound&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;"Generic animal sound"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&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;Bark&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;MakeSound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Access protected method from derived class&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Internal:&lt;/strong&gt; This modifier provides access to class members within the &lt;strong&gt;same assembly&lt;/strong&gt;, regardless of class inheritance. It's useful for sharing implementation details within a project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Imagine the 'AnimalRepository' class with an internal method 'FindAnimal'. This method can be called from any class within the same assembly.&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;class&lt;/span&gt; &lt;span class="nc"&gt;AnimalRepository&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="nf"&gt;FindAnimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Implementation details&lt;/span&gt;
    &lt;span class="p"&gt;}&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;Zoo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShowAnimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AnimalRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindAnimal&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="c1"&gt;// Access internal method from other class&lt;/span&gt;
        &lt;span class="c1"&gt;// Display animal information&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protected Internal:&lt;/strong&gt; This modifier combines the traits of protected and internal, allowing access to class members within the same assembly and derived classes, even in different assemblies. It's a hybrid modifier that strikes a harmonious balance between encapsulation and collaboration across assemblies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Consider the 'AnimalFactory' class with a protected internal method 'CreateAnimal'. This method can be called from any class within &lt;strong&gt;the same assembly or any derived class in a different assembly&lt;/strong&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AnimalFactory&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="nf"&gt;CreateAnimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Implementation details&lt;/span&gt;
    &lt;span class="p"&gt;}&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;ZooApp&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AnimalFactory&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CreateAndDisplayAnimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Animal&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;CreateAnimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Access protected internal method from derived class&lt;/span&gt;
        &lt;span class="c1"&gt;// Display animal information&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;&lt;strong&gt;Unlocking the Essence of Protected Internal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The protected internal modifier serves as a valuable asset for C# programmers, offering a balance between encapsulation and collaboration. It enables controlled sharing of implementation details across assemblies while maintaining the integrity of class hierarchies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Employ Protected Internal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The choice of the appropriate access modifier depends on the specific requirements of the class and its relationship with other classes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use protected:&lt;/strong&gt; For sharing implementation details within a class hierarchy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Utilize internal:&lt;/strong&gt; For sharing implementation details within a project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Leverage protected internal:&lt;/strong&gt; For a combination of both, enabling collaboration across assemblies while maintaining encapsulation for derived classes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Harnessing the Benefits of Protected Internal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The protected internal modifier offers a range of benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexible Access Control:&lt;/strong&gt; It allows controlled sharing of implementation details within a project while enabling access from derived classes in different assemblies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Preserving Encapsulation:&lt;/strong&gt; It upholds encapsulation principles by restricting direct access to protected internal members from outside the assembly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Promoting Code Reusability:&lt;/strong&gt; It facilitates code reuse across assemblies while maintaining the integrity of the class hierarchy.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: A Balancing Act for Success&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The protected internal access modifier stands as a testament to the versatility of access modifiers in C#. It strikes a delicate balance between encapsulation and collaboration, enabling controlled sharing of implementation details across assemblies while preserving the integrity of class hierarchies. By understanding the nuances of this modifier, developers can make informed decisions about class design and code organization, contributing to robust and maintainable software solutions.&lt;/p&gt;

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