<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elvin Asadov on Medium]]></title>
        <description><![CDATA[Stories by Elvin Asadov on Medium]]></description>
        <link>https://medium.com/@elvin-asadov?source=rss-e6e2308d431a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3tJ8DVGImPQAlhkbNisI7g.jpeg</url>
            <title>Stories by Elvin Asadov on Medium</title>
            <link>https://medium.com/@elvin-asadov?source=rss-e6e2308d431a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 18:40:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elvin-asadov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How async/await actually works in .NET]]></title>
            <link>https://medium.com/@elvin-asadov/how-async-await-actually-works-in-net-3df9b9732c36?source=rss-e6e2308d431a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3df9b9732c36</guid>
            <category><![CDATA[asycawait]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[aspnetcore]]></category>
            <dc:creator><![CDATA[Elvin Asadov]]></dc:creator>
            <pubDate>Sun, 08 Mar 2026 19:33:26 GMT</pubDate>
            <atom:updated>2026-03-08T19:33:26.524Z</atom:updated>
            <content:encoded><![CDATA[<h4>Understanding Async Internals in .NET: Worker Threads, I/O Completion Threads, and the Async State Machine</h4><p>One guy contacted me for a mock interview and one question I asked him was about async/await in C#. His answer was interesting</p><blockquote>When you write async/await, it releases the current thred and does the job on another thread from the ThreadPool. But why would it use one thread just to release it and grab another one?</blockquote><p>That’s a common misconception. So I thought why not write it down here. This is for anyone who wants to understand async better, especially if you are entering the software development field now in the <strong>AI era</strong> and want to understand the concepts deeply while still learning how real systems work in ASP.NET Core or other high concurrency platforms.</p><h4>Introduction</h4><p>If you’ve used async/await in C# and thought it “just creates new threads” you’re not alone. Many developers believe that each await spawns a thread, which can lead to misunderstandings about performance, scalability and how ASP.NET Core handles high-concurrency workloads.</p><p>In this article, we’ll dive deep into the inner workings of asynchronous programming in .NET, focusing on <strong>Tasks, threads, the async state machine, and I/O completion</strong>.</p><h4>Tasks vs Threads</h4><p>First, let’s clarify the difference:</p><ul><li><strong>Thread</strong>: A physical execution resource provided by the OS. Threads are expensive to create and manage.</li><li><strong>Task</strong>: A logical unit of work. A Task may execute on a thread, but it does <strong>not guarantee a dedicated thread</strong>.</li></ul><pre>// CPU-bound task<br>Task.Run(() =&gt; ComputeHeavyStuff());<br><br>// I/O-bound task<br>var result = await File.ReadAllTextAsync(&quot;file.json&quot;);</pre><p>Tasks represent “what to do,” while threads represent “where it runs.”</p><h4>CPU-Bound Work and Worker Threads</h4><p>CPU-bound work (heavy calculations, object processing) typically runs on <strong>ThreadPool worker threads</strong>.</p><p>The ThreadPool maintains a pool of threads. Threads are reused to avoid expensive creation costs.</p><pre>Task.Run(() =&gt; {<br>    // CPU-intensive operation<br>    for (int i = 0; i &lt; 1_000; i++) ProcessImage();<br>});</pre><p>Worker threads execute synchronous and CPU-heavy code. If many tasks are scheduled, the ThreadPool may throttle and queue tasks to avoid oversubscription.</p><h4>I/O-Bound Work and Async I/O</h4><p>I/O-bound tasks, like reading a file, making HTTP requests, or querying a database, behave differently. When you await an async I/O operation</p><pre>string text = await File.ReadAllTextAsync(&quot;file.json&quot;);</pre><ul><li>The <strong>worker thread that started the operation is freed immediately</strong>.</li><li>The OS handles the actual I/O in the background.</li><li>No thread is “blocked” while waiting.</li></ul><p>This allows thousands of I/O operations to scale efficiently without creating thousands of threads.</p><h4>I/O Completion Ports and Completion Threads</h4><p>On Windows, the OS uses <strong>I/O Completion Ports (IOCP)</strong> to signal completed operations.</p><ul><li><strong>Completion threads</strong> are ThreadPool threads dedicated to handling I/O notifications.</li><li>Their job it to mark the Task as completed and invoke the continuation.</li></ul><h4>The Async State Machine</h4><p>C# transforms async methods into a <strong>state machine</strong> implementing IAsyncStateMachine. Example:</p><pre>public async Task&lt;string&gt; ReadFileAsync()<br>{<br>    string text = await File.ReadAllTextAsync(&quot;file.txt&quot;);<br>    return text;<br>}</pre><p>Compiler lowers it to something like: <a href="https://gist.github.com/aemloviji/845b7ee72e2ba8691b5f8d7e7b0567eb">https://gist.github.com/aemloviji/845b7ee72e2ba8691b5f8d7e7b0567eb</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4160bf453f401636cf834eb04f8cb16f/href">https://medium.com/media/4160bf453f401636cf834eb04f8cb16f/href</a></iframe><h4>Continuations and Task Completion</h4><p>When the OS completes the I/O</p><ol><li>The I/O completion thread picks up the event.</li><li>The Task associated with the I/O operation is marked <strong>completed</strong>.</li><li>The stored continuation is invoked.</li><li>Execution resumes exactly after the await.</li></ol><h4>End-to-End Async Flow</h4><p>Let’s combine all of this</p><figure><img alt="@startuml
 !theme vibrant
 title Async Request Flow in .NET
 
 actor Client
 
 Client -&gt; “ASP.NET Worker Thread” : Sends request
 “ASP.NET Worker Thread” -&gt; “Async I/O Operation” : Starts async I/O (like ReadAsync)
 note right
 Worker thread is freed immediately after await
 during I/O operation
 end note
 
 “Async I/O Operation” -&gt; “OS I/O Notification” : OS performs I/O in background
 note right
 Windows: I/O Completion Ports (IOCP)
 end note
 
 “OS I/O Notification” -&gt; “ThreadPool Completion" src="https://cdn-images-1.medium.com/max/1024/0*47wbDHs40keNT9SB" /></figure><h4>Why Async Scales Better</h4><ul><li>Threads are expensive, blocking threads for I/O is wasteful.</li><li>Async I/O allows <strong>thousands of operations without thousands of threads</strong>.</li><li>Completion threads do minimal work, just wake up and resume the continuation</li></ul><h4>What You Should Remember</h4><ul><li><strong>Tasks aren’t threads. A </strong>task represents a unit of work, while threds are the resources that execute that work.</li><li><strong>Worker threads</strong> handle CPU-intensive operations like caluclations and object processing.</li><li><strong>I/O completion threads</strong> wait for the OS to signal that an I/O operation is finished and then trigger the continuation of your async code.</li><li>The <strong>async state machine</strong> turns your async method into a resumable object on the heap, preserving local variables across awaits.</li><li>Using <strong>await</strong> doesn’t block threads. The code after the await runs when the Task completes.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3df9b9732c36" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ideal methods in C#]]></title>
            <link>https://medium.com/@elvin-asadov/ideal-methods-in-c-35f4558a1520?source=rss-e6e2308d431a------2</link>
            <guid isPermaLink="false">https://medium.com/p/35f4558a1520</guid>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Elvin Asadov]]></dc:creator>
            <pubDate>Sat, 18 Feb 2023 13:26:13 GMT</pubDate>
            <atom:updated>2023-02-18T17:08:51.959Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*FyxPzSIQOKII7qkSe2mjQA.png" /><figcaption>Clean code</figcaption></figure><p>We (<em>developers</em>) must follow coding principles in order to develop maintainable, reliable, well-structured and <strong>of course readable applications</strong>. There are several coding principles such as SOLID, YAGNI, KISS, DRY that every developer (<em>actually, if you are one who cares</em>) should know about, and these principles help you to build better applications.</p><blockquote>Clean code always looks like it was written by someone who cares.</blockquote><blockquote>© Michael Feathers.</blockquote><p>In this post, I am not going to talk about Principles, but about <strong>Ideal methods</strong>. Ideal method is the method which doesn’t have any parameters. It’s also good to go if we create methods with 1 or 2 parameters. Do you know how the methods are called based on their parameters count? Here is the list:</p><ul><li>📌 <strong>Niladic</strong> (ideal method)— Methods without any parameters.</li><li>📌 <strong>Monadic </strong>— Methods with only one parameter.</li><li>📌 <strong>Dyadic </strong>— Methods with 2 parameters.</li><li>📌 <strong>Triadic </strong>— Methods with 3 parameters.</li><li>📌 <strong>Polyadic </strong>— Methods with more than 3 parameters.</li></ul><p>In ideal world <strong>we should do our best to avoid</strong> any <strong>Polyadic </strong>and even <strong>Triadic </strong>methods<strong>. </strong>Because it makes your code easier to read and understand. Triadic or Polyadic method is <strong>code smell</strong> and often indicate a violation of the Single Responsibility Principle. Problem here is not only about the bad coding practices, but the method which has more than 2 parameters makes developers feel stress and even it irritates them even if they don’t tell it directly (but sometimes you feel your👂).</p><blockquote>In computer programming, a code smell is any characteristic in the source code of a program that possibly indicates a deeper problem.</blockquote><blockquote><a href="https://en.wikipedia.org/wiki/Code_smell#:~:text=In%20computer%20programming%2C%20a%20code,%2C%20developer%2C%20and%20development%20methodology">https://en.wikipedia.org/wiki/Code_smell</a></blockquote><p>Let’s see a bad example with polyadic method. Imagine we have a method which takes information about Student and process it.</p><pre>public void SaveStudent(<br>  int id, <br>  string firstName, <br>  string lastName, <br>  int age,<br>  string email,<br>  string phoneNumber, <br>  string address)<br>{<br>   // Do some work ...<br>}</pre><p>While working with this method you really feel stress and make more focus on parameters order. Or even in some cases, intelligence approach irritates you and you will probably go to the implementation. So, in order to care about our collogues, we can change this method onto <strong>Monadic </strong>method shown below. For that reason, we need to create a type called <strong>StudenInfo </strong>and pass it to <strong>SaveStudent </strong>method.</p><pre>public sealed class StudentInfo<br>{<br>  public int Id { get; set; } <br>  public string FirstName { get; set; }<br>  public string LastName { get; set; } <br>  public int Age { get; set; }<br>  public string Email { get; set; }<br>  public string PhoneNumber { get; set; }<br>  public string Address { get; set; }<br>}<br><br>public void SaveStudent(StudentInfo student)<br>{<br>   // Do some work ...<br>}</pre><p>The approach we used in above example is called <strong>Parameter Object Pattern.</strong></p><p>📚 <strong>Read more:</strong></p><ul><li><a href="https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29">Clean code</a> — github.com</li><li><a href="https://refactoring.guru/refactoring/smells">Code smell</a> — refactoring.guru</li><li><a href="https://www.boldare.com/blog/kiss-yagni-dry-principles/">KISS, YAGNI, DRY</a> — boldare.com</li><li><a href="https://refactoring.guru/introduce-parameter-object">Parameter Object Pattern </a>— refactoring.guru</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35f4558a1520" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The code review process]]></title>
            <link>https://medium.com/@elvin-asadov/the-code-review-process-5d982a27826f?source=rss-e6e2308d431a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d982a27826f</guid>
            <category><![CDATA[developer-experience]]></category>
            <category><![CDATA[pull-request]]></category>
            <category><![CDATA[code-review]]></category>
            <dc:creator><![CDATA[Elvin Asadov]]></dc:creator>
            <pubDate>Sun, 12 Feb 2023 18:50:05 GMT</pubDate>
            <atom:updated>2023-02-12T18:50:05.604Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*_XMpngT27eZEX6hLOVpUPQ.png" /></figure><p>Code quality is very important for any project. Code review is a process targeted to improve the overall code quality. It helps all developers to follow the same coding standards and guidelines. Without this process it can be problematic to protect your code from being messy.</p><p>The code review process starts when you issue a <a href="https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests">PR (pull request)</a>. Before issuing any PR, make sure,</p><ul><li>✅Code compiles</li><li>✅No failing tests (Unit test, Integration tests)</li><li>✅Code only contains the code which is required for the feature. <a href="https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it#:~:text=%22You%20aren&#39;t%20gonna%20need%20it%22,that%20you%20%5Bwill%5D%20need%20them.%22">YAGNI</a></li><li>✅Coding standards are followed.</li></ul><p>There are 2 roles involved in PR. <strong>Reviewer(s) </strong>and <strong>Reviewee</strong>. The people who involved in this process should understand that they should be polite and don’t offend each other. When you offend someone, it can also negatively impact the team by creating depressing atmosphere. Even the worse think that can happen with offended member and even other team members is that they can leave for a new company. What we can do as a reviewer/reviewee, to prevent this happening.</p><p>As a <strong>Reviewer</strong></p><ul><li>📌You should improve your soft skills. You must be really very encouraging leader who is able to provide <a href="https://www.valamis.com/hub/constructive-feedback#what-is-constructive-feedback"><strong>constructive </strong>feedback</a>.</li><li>📌You really should understand <a href="https://www.makeuseof.com/tag/basic-programming-principles/">coding principles</a> (e.g., DRY, KISS, SOLID, YAGNI and etc.) in order to attach some details about problem to your feedback.</li></ul><p>👤As a <strong>Reviewee</strong></p><ul><li>📌You must be very polite and communicate constructively.</li><li>📌If you get positive feedback, it’s great. But in case you get any negative feedback from reviewer(s) don’t get offended and don’t take it personally.</li><li>📌And yes, if you really care about your reviewer make sure you follow YAGNI principle in mind and make push small changes instead of enormous changes in a single PR.</li></ul><p>🆀 <strong>What is constructive feedback?</strong></p><blockquote><strong>Constructive feedback</strong> is the type of feedback aimed at achieving a positive outcome by providing someone with comments, advice, or suggestions that are useful for their work or their future.</blockquote><blockquote><strong>Good constructive</strong> feedback should focus on the work rather than being a personal negative attack against an individual.</blockquote><blockquote><strong>src: </strong><a href="https://www.valamis.com/hub/constructive-feedback#what-is-constructive-feedback">https://www.valamis.com/hub/constructive-feedback#what-is-constructive-feedback</a></blockquote><p>🆀<strong> What to review?</strong></p><p>Below aspects have to be reviewed. But the list below can be extended by your company policies.</p><ul><li>✅Naming conventions</li><li>✅Does the code still performs well</li><li>✅Data is validated correctly</li><li>✅Code is proper and doesn’t contain any security hole</li><li>✅Secrets are not checked into codebase</li><li>✅New code changes are covered by tests</li></ul><p>If you want to read more about “what to review?” section, I recommend following link <a href="https://google.github.io/eng-practices/review/reviewer/looking-for.html">google.github.io (What to look for in a code review)</a>.</p><p>The simplified diagram below shows the code review process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*ZRPneOpwnLxqOX7USlJWyQ.png" /></figure><p>📚<strong>Further reading:</strong></p><ul><li><a href="https://snyk.io/learn/code-quality/#:~:text=Quality%20code%20is%20often%20defined,readability%2C%20security%2C%20and%20more.">The Importance of Code Quality</a></li><li><a href="https://about.gitlab.com/topics/version-control/what-is-code-review/#:~:text=Code%20reviews%2C%20also%20known%20as,developers%20learn%20the%20source%20code.">What is a code review?</a></li></ul><p>©️<strong> Attributes:</strong></p><p><a href="https://www.flaticon.com/free-icons/code-review">154 Free icons of code review</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d982a27826f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrency vs Parallel vs Async in .NET]]></title>
            <link>https://medium.com/@elvin-asadov/concurrency-vs-parallel-vs-async-in-net-58d1773c22d9?source=rss-e6e2308d431a------2</link>
            <guid isPermaLink="false">https://medium.com/p/58d1773c22d9</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Elvin Asadov]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 15:46:37 GMT</pubDate>
            <atom:updated>2022-12-23T15:46:37.835Z</atom:updated>
            <content:encoded><![CDATA[<h3>Concurrency vs Parallel vs Async in .NET</h3><p>After getting the same question again and again I tried to put here answer to question what a difference between Concurrency<em>, Parallel and Async in .NET</em>. Answer does not include any diagrams and tech. terminology but instead explains it in simple human language.</p><p><strong>Non-Concurrent System</strong><br>My friend (Jamal) started a company, and he does all the thing: Being a BA, QA and Developer (didn’t hire me :-)).</p><p>This kind of system (in this case business process) is non concurrent system.</p><p><strong>Concurrent System</strong><br>More customers come and sign off contract for new projects. Hense, Jamal decided to hire 1 BA, 1 QA and 1 DEV. He just enjoys doing the management things.</p><p>Now, at the same time, Jamal gets new contracts while other team members are involved in development:</p><p>Now we have a <strong>concurrency</strong> system</p><p><strong>Parallel System</strong><br>The number of customers continues to increase. 1 developer is not enough. Jamal hires one more Developer.</p><ul><li>Developer #1 works on project from customer #A</li><li>Developer #2 works on project from customer #B</li></ul><p>Development now divided between 2 developers.</p><p>Now we have <strong>Parallel</strong> system.</p><p><strong>async/await</strong><br>Developer #1 after developing a new feature asks QA to test the feature in order to close the task. At this point Developer #1 just waiting for the QA. After getting approvement from QA Developer #1 moves to a new task.</p><p>Jamal wants to improve the process and he ask Developers to stop waiting. After the feature is tested, the QA will inform and either Developer #1 or #2 can close the task.</p><p>It’s <strong>async.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58d1773c22d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lowering in C#]]></title>
            <link>https://medium.com/@elvin-asadov/lowering-in-c-c456995c4ff0?source=rss-e6e2308d431a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c456995c4ff0</guid>
            <dc:creator><![CDATA[Elvin Asadov]]></dc:creator>
            <pubDate>Wed, 07 Dec 2022 15:13:10 GMT</pubDate>
            <atom:updated>2022-12-08T06:37:05.735Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*lfDEROqF6juy0X6Ld5LaVw.png" /></figure><p>Hello everybody!</p><p>In this post, I want to talk about the <strong>lowering</strong> feature of the C# compiler. Simply saying, it’s a technique defined in the C# compiler to rewrite more complex semantic constructs in terms of simpler ones. It’s a part of the compilation process and it happens before our code is converted into IL code. For example:</p><pre><strong>1. foreach/for</strong> will be lowered to <strong>while<br>2. record </strong>will be lowered to<strong> class <br><br></strong>and etc.</pre><p>I highly recommend reading about <a href="https://mattwarren.org/2017/05/25/Lowering-in-the-C-Compiler/">lowering in the C# compiler</a> but in this post, I will focus on explaining how the record type is implemented in C#.</p><blockquote>C# 9 introduces <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record"><em>records</em></a>, a new reference type that you can create instead of classes or structs. C# 10 adds <em>record structs</em> so that you can define records as value types. <strong>Records are distinct from classes in that record types use <em>value-based equality</em>. Two variables of a record type are equal if the record type definitions are identical, and if for every field, the values in both records are equal.</strong> Two variables of a class type are equal if the objects referred to are the same class type and the variables refer to the same object. Value-based equality implies other capabilities you’ll probably want in record types. The compiler generates many of those members when you declare a <em>record</em> instead of a <em>class</em>. The compiler generates those same methods for <em>record struct</em> types.</blockquote><blockquote>ref: <a href="https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/records">Use record types — C# tutorial | Microsoft Learn</a></blockquote><p>To explain the lowering feature for the record types, let’s use the code below</p><pre>// gist: https://gist.github.com/AEMLoviji/b97b53e28b968d468465ceb3976bede8<br><br>// STEP 1<br>public record Device(string Manufacturer, string Identifier);<br><br>// STEP 2<br>var device1 = new Device(&quot;DELL&quot;, &quot;f8078ddc-2d59-4902-9acd-8cd88649186d&quot;);<br><br>// STEP 3<br>var device2 = device1 with <br>{<br>  Identifier = &quot;aea8be94-788a-4b01-a569-9f496904a49c&quot; <br>};</pre><p><strong>STEP 1</strong>: Create a record type called <strong>Device </strong>with 2 properties <strong>Manufacturer</strong> and <strong>Identifier.</strong></p><p><strong>STEP 2</strong>: Create an instance of the Device type</p><p><strong>STEP 3</strong>: Create an instance by copying device1 with some modification using <em>with</em> expression to achieve <em>non-destructive mutation</em></p><p>To understand how the <em>record type</em> and <em>with expression</em>is handled by the C# compiler we can you an online tool called <a href="https://sharplab.io/">https://sharplab.io/</a> to see how the feature is transformed into a low-level feature. To be concrete, in the case of record type it’s turned into the class which we are all familiar with.</p><blockquote>SharpLab is a .NET code playground that shows intermediate steps and results of code compilation. Some language features are thin wrappers on top of other features — e.g., using() becomes try/finally. SharpLab allows you to see the code as compiler sees it and get a better understanding of .NET languages.</blockquote><blockquote>ref: <a href="https://github.com/ashmind/SharpLab">GitHub — ashmind/SharpLab: .NET language playground</a></blockquote><p>When lowering started the Device record type is turned into the simple Device class shown below. For simplicity, some generated code pieces were deducted.</p><pre>// to see full code please navigate to the link below<br>// https://gist.github.com/AEMLoviji/dd74d6c1b500bbe85c63bf82cd507f12<br><br>[CompilerGenerated]<br>internal class Program<br>{<br>    private static void &lt;Main&gt;$(string[] args)<br>    {<br>        new Device(&quot;DELL&quot;, &quot;f8078ddc-2d59-4902-9acd-8cd88649186d&quot;)<br>          .&lt;Clone&gt;$()<br>          .Identifier = &quot;aea8be94-788a-4b01-a569-9f496904a49c&quot;;<br>    }<br>}<br><br>// Device record type converted to Device class<br>public class Device : IEquatable&lt;Device&gt;<br>{<br>    [CompilerGenerated]<br>    private readonly string &lt;Manufacturer&gt;k__BackingField;<br><br>    [CompilerGenerated]<br>    private readonly string &lt;Identifier&gt;k__BackingField;<br><br>    public string Manufacturer<br>    {<br>        [CompilerGenerated]<br>        get<br>        {<br>            return &lt;Manufacturer&gt;k__BackingField;<br>        }<br>        [CompilerGenerated]<br>        init<br>        {<br>            &lt;Manufacturer&gt;k__BackingField = value;<br>        }<br>    }<br><br>    public string Identifier<br>    {<br>        [CompilerGenerated]<br>        get<br>        {<br>            return &lt;Identifier&gt;k__BackingField;<br>        }<br>        [CompilerGenerated]<br>        init<br>        {<br>            &lt;Identifier&gt;k__BackingField = value;<br>        }<br>    }<br><br>    public Device(string Manufacturer, string Identifier)<br>    {<br>        &lt;Manufacturer&gt;k__BackingField = Manufacturer;<br>        &lt;Identifier&gt;k__BackingField = Identifier;<br>        base..ctor();<br>    }<br><br>    [CompilerGenerated]<br>    public virtual Device &lt;Clone&gt;$()<br>    {<br>        return new Device(this);<br>    }<br><br>    [CompilerGenerated]<br>    protected Device(Device original)<br>    {<br>        &lt;Manufacturer&gt;k__BackingField = original.&lt;Manufacturer&gt;k__BackingField;<br>        &lt;Identifier&gt;k__BackingField = original.&lt;Identifier&gt;k__BackingField;<br>    }<br><br>    [CompilerGenerated]<br>    public void Deconstruct(out string Manufacturer, out string Identifier)<br>    {<br>        Manufacturer = this.Manufacturer;<br>        Identifier = this.Identifier;<br>    }<br>}</pre><p>As you can see, the first thing the C# compiler does is convert the record type to class. And the answer to the question, of how <em>with expression</em> is understood by the C# compiler is that during the lowering process the C# compiler generates the virtual <em>Device &lt;Clone&gt;$()</em> method in the Device class generated by the compiler and replaces <em>with expression</em> with the <em>&lt;Clone&gt;$()</em> method. And finally, the lowered version of the code will be compiled into IL code.</p><p>Bonus reading: <a href="https://www.reddit.com/r/csharp/comments/fbrtb2/lowering_in_c_whats_really_going_on_in_your_code/">Lowering in C#: What’s really going on in your code?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c456995c4ff0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CLR Finalization Mechanism]]></title>
            <link>https://medium.com/@elvin-asadov/clr-finalization-mechanism-6ee62f8d3afe?source=rss-e6e2308d431a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ee62f8d3afe</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Elvin Asadov]]></dc:creator>
            <pubDate>Mon, 21 Nov 2022 11:04:02 GMT</pubDate>
            <atom:updated>2022-11-21T11:09:34.720Z</atom:updated>
            <content:encoded><![CDATA[<p>The image below shows how the objects with <strong>Finalize </strong>method defined are handled by GC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3b7_CCURQtSvKsmPi65EIQ.png" /></figure><p><strong>TODO</strong>: Add some text to explain the image above.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ee62f8d3afe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C# required members]]></title>
            <link>https://medium.com/@elvin-asadov/csharp-required-members-7faa86431eed?source=rss-e6e2308d431a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7faa86431eed</guid>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Elvin Asadov]]></dc:creator>
            <pubDate>Thu, 17 Nov 2022 14:38:32 GMT</pubDate>
            <atom:updated>2022-11-17T14:55:35.080Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/131/1*5BCr2Y_IrQekCAKlNAM7-g.png" /><figcaption>C Sharp 11</figcaption></figure><p>The required modifier is available beginning with C# 11.</p><blockquote>The <strong>required </strong>modifier can be used in a <strong>field </strong>or <strong>property </strong>member to ensure that we have to explicitly set a value when an object is initialized by object initializer.</blockquote><pre>// Object initializer in C#<br>AnyType type = new()<br>{<br>  // Initialize the properties<br>};</pre><p>Before C# 11 when creating objects that used object initializers, you didn’t use to be able to specify that some properties/fields must be initialized. Now, you can do it just putting <strong>required </strong>modifier before any field or property. See an example.</p><pre>public sealed class User<br>{<br>    // see how `required` modifier is used<br>    public required string FullName { get; init; }<br><br>    public int Age {get; set;}<br>}</pre><p>When we initialize an object, we have to ensure that we set a value to each required property by an object initializer. Otherwise, the C# compiler will give an error if you try to initialize an object without initializing the required property.</p><pre>User user = new() { Age = 100 }; <br>// Error CS9035: Required member &#39;User.FullName&#39;<br>// must be set in the object initializer or attribute constructor.</pre><p>Check the docs for more: <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/required-members">required members</a> or <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required">required modifier</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7faa86431eed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>