<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Csharp on Ravindra Devrani</title><link>https://ravindradevrani.com/tags/csharp/</link><description>Recent content in Csharp on Ravindra Devrani</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 29 Jul 2025 18:24:07 +0530</lastBuildDate><atom:link href="https://ravindradevrani.com/tags/csharp/index.xml" rel="self" type="application/rss+xml"/><item><title>Asynchronous Programming in C# - Comparing it with multi-threading</title><link>https://ravindradevrani.com/posts/asynchronous-programming-in-csharp-and-how-is-it-different-from-multithreading/</link><pubDate>Tue, 29 Jul 2025 18:24:07 +0530</pubDate><guid>https://ravindradevrani.com/posts/asynchronous-programming-in-csharp-and-how-is-it-different-from-multithreading/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>&lt;code>Asynchronous task&lt;/code> is a &lt;strong>non-blocking&lt;/strong> task. Main thread goes back to the &lt;strong>thread pool&lt;/strong> (and free to do other tasks) when it reaches to await &amp;ndash; and new thread is assigned when wait is complete.&lt;/p>
&lt;p>It is different from the multi-threading. In multi-threding, task is divided between multiple threads. Cores of your CPU are utilized.&lt;/p>
&lt;h2 id="analogy">Analogy&lt;/h2>
&lt;p>Let&amp;rsquo;s say you have some chores like:&lt;/p>
&lt;ol>
&lt;li>Boiling eggs 🫕 (or sweet potatoes🍠🍠 if you dont eat eggs 🥚🥚)&lt;/li>
&lt;li>Clean 🧹🪣 the house&lt;/li>
&lt;/ol>
&lt;p>There are multiple ways to achieve this:&lt;/p></description></item><item><title>C#: Byte and Its Use Cases</title><link>https://ravindradevrani.com/posts/byte-and-its-use-cases-in-csharp/</link><pubDate>Sat, 17 May 2025 17:32:06 +0530</pubDate><guid>https://ravindradevrani.com/posts/byte-and-its-use-cases-in-csharp/</guid><description>&lt;!-- raw HTML omitted -->
&lt;ul>
&lt;li>&lt;code>byte&lt;/code> is a value type in c#&lt;/li>
&lt;li>&lt;code>byte&lt;/code> is an unsigned integer which stores value from 0 to 255, which means it can not store negetive numbers.&lt;/li>
&lt;li>Size of &lt;code>byte&lt;/code> is 8-bit (1 byte)&lt;/li>
&lt;li>CTS equivalent of byte is &lt;code>System.Byte&lt;/code>&lt;/li>
&lt;li>It&amp;rsquo;s default value is 0&lt;/li>
&lt;li>It is great for memory optimization when dealing with small numbers.&lt;/li>
&lt;/ul>
&lt;p>Examples:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">byte&lt;/span> a = &lt;span style="color:#ae81ff">10&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Console.WriteLine(a);
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="use-cases">Use cases&lt;/h2>
&lt;ol>
&lt;li>Working with ASCII value&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">byte&lt;/span> asciiValue = (&lt;span style="color:#66d9ef">byte&lt;/span>)&lt;span style="color:#e6db74">&amp;#39;R&amp;#39;&lt;/span>; &lt;span style="color:#75715e">// 82&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">byte&lt;/span> asciiValue2 = (&lt;span style="color:#66d9ef">byte&lt;/span>)&lt;span style="color:#e6db74">&amp;#39;r&amp;#39;&lt;/span>; &lt;span style="color:#75715e">// 114&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ol start="2">
&lt;li>Encoding text into bytes&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">using&lt;/span> System.Text;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">string&lt;/span> message = &lt;span style="color:#e6db74">&amp;#34;Ravindra&amp;#34;&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">byte&lt;/span>[] encoded = Encoding.UTF8.GetBytes(message);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Console.WriteLine(&lt;span style="color:#e6db74">$&amp;#34;{string.Join(&amp;#34;&lt;/span>,&lt;span style="color:#e6db74">&amp;#34;,encoded)}&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// op: 82,97,118,105,110,100,114,97&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Note: Each character is encoded using UTF-8. For standard letters, the byte values match their ASCII codes.. (R: 82, a:97, v:118 &amp;hellip;)&lt;/p></description></item><item><title>IEnumerable Vs IQueryable In C#</title><link>https://ravindradevrani.com/posts/ienumerable-vs-iqueryable/</link><pubDate>Tue, 13 May 2025 11:45:17 +0530</pubDate><guid>https://ravindradevrani.com/posts/ienumerable-vs-iqueryable/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>There has been a discussion around town about the difference between an &lt;code>IEnumerable&lt;/code> and an &lt;code>IQueryable&lt;/code>, especially in c# interviews. I won&amp;rsquo;t be diving into the fact that &lt;code>IEnumerable&lt;/code> is part of the &lt;code>System.Collections&lt;/code> namespace and &lt;code>IQueryable&lt;/code> belongs to &lt;code>System.Linq&lt;/code> namespace (or did I???). Rather, I’ll focus on the practical usage of both—how they work, and when to use each.&lt;/p>
&lt;h2 id="iqueryable">IQueryable&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> IActionResult GetPeople()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// It will retrieve 2 records from database &lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> IQueryable&amp;lt;Person&amp;gt; people = _context.People.Take(&lt;span style="color:#ae81ff">2&lt;/span>); 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Note: At the above line, no data will be retrieved from the database&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> Ok(people); &lt;span style="color:#75715e">// Data will be retrieved here&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="corresponding-sql">Corresponding sql&lt;/h3>
&lt;p>Note that, I am using &lt;code>Sqlite&lt;/code>, the above code is translated to this query:&lt;/p></description></item><item><title>LINQ: Zip() operator</title><link>https://ravindradevrani.com/posts/linq-zip-method/</link><pubDate>Tue, 25 Feb 2025 10:10:41 +0530</pubDate><guid>https://ravindradevrani.com/posts/linq-zip-method/</guid><description>&lt;!-- raw HTML omitted -->
&lt;blockquote>
&lt;p>There is also a &lt;a href="https://youtu.be/p1SXSMe0wk8?si=8p2ZptMcPrI_fodS">video version&lt;/a> of this tutorial.&lt;/p>
&lt;/blockquote>
&lt;p>Let&amp;rsquo;s understand it with example.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">int&lt;/span>[] nums1 = [&lt;span style="color:#ae81ff">1&lt;/span>, &lt;span style="color:#ae81ff">2&lt;/span>, &lt;span style="color:#ae81ff">3&lt;/span>, &lt;span style="color:#ae81ff">4&lt;/span>];
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">int&lt;/span>[] nums2 = [&lt;span style="color:#ae81ff">3&lt;/span>, &lt;span style="color:#ae81ff">4&lt;/span>, &lt;span style="color:#ae81ff">5&lt;/span>, &lt;span style="color:#ae81ff">6&lt;/span>];
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>IEnumerable&amp;lt;&lt;span style="color:#66d9ef">int&lt;/span>&amp;gt;? product = nums1.Zip(nums2, (n1, n2) =&amp;gt; n1 * n2);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Console.WriteLine(&lt;span style="color:#66d9ef">string&lt;/span>.Join(&lt;span style="color:#e6db74">&amp;#34;, &amp;#34;&lt;/span>, product)); &lt;span style="color:#75715e">// 3, 8, 15, 24&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Let&amp;rsquo;s break it down:&lt;/p>
&lt;p>&lt;code>IEnumerable&amp;lt;int&amp;gt;? product = nums1.Zip(nums2, (n1, n2) =&amp;gt; n1 * n2);&lt;/code>&lt;/p>
&lt;p>It takes &lt;code>nums1[i]&lt;/code> and &lt;code>nums2[i]&lt;/code>, evaluates it (nums1[0]*nums2[0]) and returns it. Here &lt;code>i&lt;/code> is the index of the array. For example.&lt;/p></description></item><item><title>SingleAsync vs SingleOrDefaultAync vs FirstAsync vs FirstOrDefaultAsync vs FindAync</title><link>https://ravindradevrani.com/posts/singleasync-vs-singleordefaultaync-vs-firstasync-vs-firstordefaultasync-vs-findaync/</link><pubDate>Mon, 24 Feb 2025 18:07:04 +0530</pubDate><guid>https://ravindradevrani.com/posts/singleasync-vs-singleordefaultaync-vs-firstasync-vs-firstordefaultasync-vs-findaync/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>I don’t think there is any need of introduction. Let’s jump to the code section. We coders understand with code more. Let’s understand the concept then you don’t need to remember any definition.&lt;/p>
&lt;p>This is the recordset against which I am running queries.&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>Id&lt;/th>
 &lt;th>FirstName&lt;/th>
 &lt;th>LastName&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>1&lt;/td>
 &lt;td>John&lt;/td>
 &lt;td>Doe&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>2&lt;/td>
 &lt;td>Ravindra&lt;/td>
 &lt;td>Devrani&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>3&lt;/td>
 &lt;td>Mohan&lt;/td>
 &lt;td>Singh&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>20&lt;/td>
 &lt;td>Mahesh&lt;/td>
 &lt;td>Soni&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>21&lt;/td>
 &lt;td>John&lt;/td>
 &lt;td>Snow&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>First and foremost let’s say we need a record with LastName=”Doe”.&lt;/p></description></item><item><title>Curious Case of LINQ Group By</title><link>https://ravindradevrani.com/posts/curious-case-of-linq-group-by/</link><pubDate>Mon, 24 Feb 2025 16:17:33 +0530</pubDate><guid>https://ravindradevrani.com/posts/curious-case-of-linq-group-by/</guid><description>&lt;!-- raw HTML omitted -->
&lt;h3 id="schema">Schema&lt;/h3>
&lt;ul>
&lt;li>Department (DepartmentId, Name)&lt;/li>
&lt;li>Employee (EmployeeId, Name, DepartmentId)&lt;/li>
&lt;/ul>
&lt;h3 id="result-set-ineed">Result set I need&lt;/h3>
&lt;p>Show all departments with total number of employees. Do not skip the departments which have 0 employees. As shown below:&lt;/p>
&lt;p>DepartmentIdNameTotalEmployees1Engineering22Marketing13HR0&lt;/p>
&lt;p>I have applied various queries and checked their equivalent sql.&lt;/p>
&lt;h3 id="1-straightforward-but-skips-the-department-which-has-no-employees">1. Straightforward but skips the department which has no employees&lt;/h3>
&lt;p>This query does not meet my requirement. It would be a good choice if I don’t need departments without any employees.&lt;/p></description></item><item><title>LINQ: SelectMany()</title><link>https://ravindradevrani.com/posts/select-many-linq/</link><pubDate>Mon, 24 Feb 2025 16:09:29 +0530</pubDate><guid>https://ravindradevrani.com/posts/select-many-linq/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>NOTE: You can find the source code &lt;a href="https://github.com/rd003/DotnetPracticeDemos/tree/master/SelectManyDemo/SelectManyDemo" title="https://github.com/rd003/DotnetPracticeDemos/tree/master/SelectManyDemo/SelectManyDemo">here&lt;/a>.&lt;/p>
&lt;h3 id="schema-overview">Schema Overview&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Department&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Department&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> DepartmentId { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span> Name { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = &lt;span style="color:#66d9ef">string&lt;/span>.Empty;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> ICollection&amp;lt;Employee&amp;gt; Employees { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = [];
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Employee&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Employee&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> EmployeeId { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span> Name { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = &lt;span style="color:#66d9ef">string&lt;/span>.Empty;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> DepartmentId { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> Department Department { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = &lt;span style="color:#66d9ef">null&lt;/span>!;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> ICollection&amp;lt;EmployeeSkill&amp;gt; EmployeeSkills { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = [];
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Skills&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Skill&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> SkillId { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span> Name { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = &lt;span style="color:#66d9ef">string&lt;/span>.Empty;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> ICollection&amp;lt;EmployeeSkill&amp;gt; EmployeeSkills { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = [];
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// EmployeeSkills (Junction table)&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">EmployeeSkill&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> EmployeeId { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> SkillId { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> Employee Employee { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = &lt;span style="color:#66d9ef">null&lt;/span>!;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> Skill Skill { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>; } = &lt;span style="color:#66d9ef">null&lt;/span>!;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>In simpler terms:&lt;/p></description></item><item><title>Higher Order Functions in C#</title><link>https://ravindradevrani.com/posts/higher-order-functions-in-csharp/</link><pubDate>Sun, 23 Feb 2025 13:07:13 +0530</pubDate><guid>https://ravindradevrani.com/posts/higher-order-functions-in-csharp/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>&lt;strong>Higher-order functions (HOF)&lt;/strong> are functions that can take a function as an argument or return a function or both. In C#, higher-order functions are achieved using delegates, lambda expressions and expression trees.&lt;/p>
&lt;h2 id="example-1-using-adelegate">Example 1: Using a Delegate&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Declare a delegate that takes an int and returns an int&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">delegate&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> IntOperation(&lt;span style="color:#66d9ef">int&lt;/span> x);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Program&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Higher-order function that takes a delegate as an argument&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">static&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> PerformOperation(&lt;span style="color:#66d9ef">int&lt;/span> &lt;span style="color:#66d9ef">value&lt;/span>, IntOperation operation)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> operation(&lt;span style="color:#66d9ef">value&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">static&lt;/span> &lt;span style="color:#66d9ef">void&lt;/span> Main()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Create a delegate instance that points to the Square method&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> IntOperation square = x =&amp;gt; x &lt;span style="color:#960050;background-color:#1e0010">\&lt;/span>* x;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Pass the delegate to the higher-order function&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">int&lt;/span> result = PerformOperation(&lt;span style="color:#ae81ff">5&lt;/span>, square);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Console.WriteLine(result); &lt;span style="color:#75715e">// Output: 25&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="example-2-using-lambda-expression">Example 2 : Using lambda expression&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Higher-order function that takes a lambda expression as an argument&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">static&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> PerformOperation(&lt;span style="color:#66d9ef">int&lt;/span> &lt;span style="color:#66d9ef">value&lt;/span>, Func&amp;lt;&lt;span style="color:#66d9ef">int&lt;/span>, &lt;span style="color:#66d9ef">int&lt;/span>&lt;span style="color:#960050;background-color:#1e0010">\&lt;/span>&amp;gt; operation)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> operation(&lt;span style="color:#66d9ef">value&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// main method&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Define a lambda expression that squares a number&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Func&amp;lt;&lt;span style="color:#66d9ef">int&lt;/span>, &lt;span style="color:#66d9ef">int&lt;/span>&amp;gt; square = x =&amp;gt; x * x;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Pass the lambda expression to the higher-order function&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">int&lt;/span> result = PerformOperation(&lt;span style="color:#ae81ff">5&lt;/span>, square);
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="example-3-function-as-a-returnvalue">Example 3: Function as a Return Value&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Program&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Higher-order function that returns a function&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">static&lt;/span> Func&amp;lt;&lt;span style="color:#66d9ef">int&lt;/span>, &lt;span style="color:#66d9ef">int&lt;/span>&lt;span style="color:#960050;background-color:#1e0010">\&lt;/span>&amp;gt; GetOperation(&lt;span style="color:#66d9ef">bool&lt;/span> isSquare)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> (isSquare)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> x =&amp;gt; x &lt;span style="color:#960050;background-color:#1e0010">\&lt;/span>* x;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">else&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> x =&amp;gt; x + x;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">static&lt;/span> &lt;span style="color:#66d9ef">void&lt;/span> Main()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Get a function that squares a number&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Func&amp;lt;&lt;span style="color:#66d9ef">int&lt;/span>, &lt;span style="color:#66d9ef">int&lt;/span>&amp;gt; operation = GetOperation(&lt;span style="color:#66d9ef">true&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Use the returned function&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">int&lt;/span> result = operation(&lt;span style="color:#ae81ff">5&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Console.WriteLine(result); &lt;span style="color:#75715e">// Output: 25&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>In c#, higher order functions are everywhere. If one have used LINQ, must have used HO functions. Collection’s Where() is the good example.&lt;/p></description></item><item><title>Let's dive into various types of properties in c#</title><link>https://ravindradevrani.com/posts/various-types-of-properties-in-csharp/</link><pubDate>Sun, 23 Feb 2025 12:48:39 +0530</pubDate><guid>https://ravindradevrani.com/posts/various-types-of-properties-in-csharp/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>I assume you have some knowledge of C# properties. If not, here’s a quick definition: &lt;strong>A property is a class member that provides a flexible way to read, write, or compute the value of a private field.&lt;/strong> Don’t worry if you’re new to properties; we’ll be using them in this blog, and I’ll explain them as we go along.&lt;/p>
&lt;h3 id="1-properties-with-getter-andsetter">1. Properties with getter and setter&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Person&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span> Name { &lt;span style="color:#66d9ef">get&lt;/span>;&lt;span style="color:#66d9ef">set&lt;/span>; }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> Age { &lt;span style="color:#66d9ef">get&lt;/span>; &lt;span style="color:#66d9ef">set&lt;/span>;}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Generally, we define properties like this. The Person class has two properties &lt;strong>Name&lt;/strong> and &lt;strong>Age,&lt;/strong> each with getters and setters. This enables you to set and read their values.&lt;/p></description></item><item><title>Understanding The Pure And Impure Functions in C#</title><link>https://ravindradevrani.com/posts/pure-impure-functions-in-csharp/</link><pubDate>Sun, 23 Feb 2025 11:38:07 +0530</pubDate><guid>https://ravindradevrani.com/posts/pure-impure-functions-in-csharp/</guid><description>&lt;!-- raw HTML omitted -->
&lt;h2 id="pure-functions">Pure functions&lt;/h2>
&lt;p>Pure function is that:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Always returns the same output&lt;/strong> given the same inputs.&lt;/li>
&lt;li>&lt;strong>Does not modify the state of object&lt;/strong> or any external state.&lt;/li>
&lt;li>&lt;strong>Does not have any side effects&lt;/strong> such as I/O operations, exceptions or modifying external variables.&lt;/li>
&lt;/ol>
&lt;ul>
&lt;li>In other words, a pure function takes input, process it and returns the output without modifying anything else.&lt;/li>
&lt;li>Pure functions are easy to test and debug.&lt;/li>
&lt;/ul>
&lt;h2 id="examples">Examples&lt;/h2>
&lt;ol>
&lt;li>Mathematical calculations&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span> Add(&lt;span style="color:#66d9ef">int&lt;/span> a, &lt;span style="color:#66d9ef">int&lt;/span> b)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> a + b;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ol start="2">
&lt;li>String manipulation&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span> Concatenate(&lt;span style="color:#66d9ef">string&lt;/span> a, &lt;span style="color:#66d9ef">string&lt;/span> b)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> a + b;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ol start="3">
&lt;li>Array operations&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span>[] SortArray(&lt;span style="color:#66d9ef">int&lt;/span>[] array)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> array.OrderBy(x =&amp;gt; x).ToArray();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ol start="4">
&lt;li>Data transformations&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span> ConvertToString(&lt;span style="color:#66d9ef">int&lt;/span> &lt;span style="color:#66d9ef">value&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#66d9ef">value&lt;/span>.ToString();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ol start="5">
&lt;li>Validation&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">bool&lt;/span> IsValidEmail(&lt;span style="color:#66d9ef">string&lt;/span> email)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> email.Contains(&lt;span style="color:#e6db74">&amp;#34;@&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="impure-functions">Impure Functions&lt;/h2>
&lt;p>Impure function is that&lt;/p></description></item><item><title>Private Constructor in C#</title><link>https://ravindradevrani.com/posts/private-constructor-in-csharp/</link><pubDate>Fri, 21 Feb 2025 10:20:08 +0530</pubDate><guid>https://ravindradevrani.com/posts/private-constructor-in-csharp/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>In C#, a private constructor is a constructor method declared with the &lt;code>private&lt;/code> access modifier (or without mentioning any modifier). If a class have only a private constructor and does not have any public constructor, then you are not able to create the instance of a class. Example 👇&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Student&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Student() { } &lt;span style="color:#75715e">// private constructor&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Program&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">static&lt;/span> &lt;span style="color:#66d9ef">void&lt;/span> Main()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Student stu = &lt;span style="color:#66d9ef">new&lt;/span>(); &lt;span style="color:#75715e">//Error CS0122 &amp;#39;Student.Student()&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// is inaccessible due to its protection level&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>When you try to create the instance of stu, you will the following error.&lt;/p></description></item><item><title>Delegates, Anonymous Method and Lambda Expression</title><link>https://ravindradevrani.com/posts/delegates-anonymous-method-and-lambda-expression/</link><pubDate>Fri, 21 Feb 2025 09:31:26 +0530</pubDate><guid>https://ravindradevrani.com/posts/delegates-anonymous-method-and-lambda-expression/</guid><description>&lt;!-- raw HTML omitted -->
&lt;ul>
&lt;li>Delegates in C# are essentially type-safe function pointers.&lt;/li>
&lt;li>They allow you to treat methods as objects, enabling you to pass methods as parameters, store them in variables, and invoke them dynamically.&lt;/li>
&lt;li>Delegates are particularly useful for implementing callback mechanisms and event handling in C#.&lt;/li>
&lt;/ul>
&lt;p>delegate void MyDelegate(string message);&lt;/p>
&lt;p>You can point this delegate to any method with similar signature.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">namespace&lt;/span> BasicsOfCSharp;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Declare a delegate type&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">delegate&lt;/span> &lt;span style="color:#66d9ef">void&lt;/span> MyDelegate(&lt;span style="color:#66d9ef">string&lt;/span> message);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Program&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Method that matches the delegate signature&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">static&lt;/span> &lt;span style="color:#66d9ef">void&lt;/span> DisplayMessage(&lt;span style="color:#66d9ef">string&lt;/span> message)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Console.WriteLine(&lt;span style="color:#e6db74">&amp;#34;Message: &amp;#34;&lt;/span> + message);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">static&lt;/span> &lt;span style="color:#66d9ef">void&lt;/span> Main()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// Instantiate the delegate.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> MyDelegate handler = DisplayMessage;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// calling the handler&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> handler(&lt;span style="color:#e6db74">&amp;#34;Hello..&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>👉Multicast Delegate:&lt;/strong>&lt;/p></description></item><item><title>Abstract Class in C#</title><link>https://ravindradevrani.com/posts/abstract-class-in-csharp/</link><pubDate>Fri, 21 Feb 2025 09:20:13 +0530</pubDate><guid>https://ravindradevrani.com/posts/abstract-class-in-csharp/</guid><description>&lt;p>In c#, Abstract class is a class that can not be instantiated on its own&lt;/p>
&lt;!-- raw HTML omitted -->
&lt;ul>
&lt;li>In c#, &lt;code>Abstract class&lt;/code> is a class that can not be instantiated on its own&lt;/li>
&lt;li>It is typically used as a &lt;code>base class&lt;/code> for other class.&lt;/li>
&lt;li>&lt;code>Abstract class&lt;/code> provides a way to achieve &lt;a href="https://medium.com/@ravindradevrani/abstraction-in-c-c3d4c832942a">&lt;code>abstraction&lt;/code>&lt;/a>, because there you can just declare the methods (abstract methods) and implement them later.&lt;/li>
&lt;li>It can contain both &lt;code>abstract methods&lt;/code> (methods without implementation details) and &lt;code>non-abstract methods&lt;/code> (method with implementation details).&lt;/li>
&lt;li>Similar goal can be achieved with &lt;code>interface&lt;/code>, but in abstract class you can also define non-abstract method. These methods are needed when you need to share some common functionality.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">public&lt;/span> &lt;span style="color:#66d9ef">abstract&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Shape&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// it is the basic syntax of abstract class&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Example:&lt;/p></description></item><item><title>Easiest Way to Handle Csv Files in Csharp</title><link>https://ravindradevrani.com/posts/easiest-way-to-handle-csv-files-in-csharp/</link><pubDate>Thu, 20 Feb 2025 17:58:49 +0530</pubDate><guid>https://ravindradevrani.com/posts/easiest-way-to-handle-csv-files-in-csharp/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>In this tutorial we will se how to read and write data to &lt;strong>csv&lt;/strong> file. It is pretty much easy if you have some external library for that. We are definitely going to use a library and that will be &lt;strong>CsvHelper&lt;/strong>.&lt;/p>
&lt;p>You can also check the video version of this tutorial.&lt;/p>
&lt;p>First and foremost, create a &lt;strong>c# console application&lt;/strong> in .net core. After that we need to install a nuget package, which is&lt;code>**CsvHelper**&lt;/code>&lt;/p></description></item><item><title>Multiple Ways to Find Duplicates in Csharp Array</title><link>https://ravindradevrani.com/posts/multiple-ways-to-find-duplicates-in-csharp-array/</link><pubDate>Thu, 20 Feb 2025 17:49:19 +0530</pubDate><guid>https://ravindradevrani.com/posts/multiple-ways-to-find-duplicates-in-csharp-array/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>In C#, we can use various approaches to find duplicate elements in array. Each have pros and cons. We will use 3 approaches in this article.&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Using a HashSet&lt;/strong>&lt;/li>
&lt;li>&lt;strong>Using a Dictionary&lt;/strong>&lt;/li>
&lt;li>&lt;strong>Using LINQ&lt;/strong>&lt;/li>
&lt;/ol>
&lt;p>Lets take this array as an example, from this array we will extract &lt;strong>distinct numbers&lt;/strong> and &lt;strong>duplicate numbers.&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cs" data-lang="cs">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">int&lt;/span>[] numbers = { &lt;span style="color:#ae81ff">4&lt;/span>,&lt;span style="color:#ae81ff">7&lt;/span>, &lt;span style="color:#ae81ff">2&lt;/span>, &lt;span style="color:#ae81ff">3&lt;/span>, &lt;span style="color:#ae81ff">4&lt;/span>, &lt;span style="color:#ae81ff">5&lt;/span>, &lt;span style="color:#ae81ff">3&lt;/span>, &lt;span style="color:#ae81ff">6&lt;/span>, &lt;span style="color:#ae81ff">7&lt;/span>, &lt;span style="color:#ae81ff">8&lt;/span>,&lt;span style="color:#ae81ff">1&lt;/span>, &lt;span style="color:#ae81ff">8&lt;/span> };
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We will use this array in all three approaches. So let’s understand one by one.&lt;/p></description></item><item><title>C# Dictionary and Its Use Cases</title><link>https://ravindradevrani.com/posts/csharp-dictionary-and-its-use-cases/</link><pubDate>Thu, 20 Feb 2025 17:40:42 +0530</pubDate><guid>https://ravindradevrani.com/posts/csharp-dictionary-and-its-use-cases/</guid><description>&lt;!-- raw HTML omitted -->
&lt;h2 id="what-is-dictionary">What is dictionary?&lt;/h2>
&lt;p>Dictionary is a collection, that store the value in the form of key value pair. It allows you to quick access of value using the key. This data structure is widely used in programming because of its fast look-up time, which makes it ideal for applications that require quick data retrieval&lt;/p>
&lt;p>👉 You can not add duplicate key in dictionary&lt;/p>
&lt;p>&lt;strong>Creating a dictionary:&lt;/strong>&lt;/p></description></item></channel></rss>