<?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: Vidisha Parab</title>
    <description>The latest articles on DEV Community by Vidisha Parab (@vparab).</description>
    <link>https://dev.to/vparab</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%2F289500%2Fa22f9ad9-660e-4fc0-a10e-20889bdeb788.jpeg</url>
      <title>DEV Community: Vidisha Parab</title>
      <link>https://dev.to/vparab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vparab"/>
    <language>en</language>
    <item>
      <title>Working with JsonObject, JsonNode, JsonValue and JsonArray [System.Text.Json API]</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Mon, 20 May 2024 08:58:19 +0000</pubDate>
      <link>https://dev.to/vparab/working-with-jsonobject-jsonnode-jsonvalue-and-jsonarray-systemtextjson-api-5b8l</link>
      <guid>https://dev.to/vparab/working-with-jsonobject-jsonnode-jsonvalue-and-jsonarray-systemtextjson-api-5b8l</guid>
      <description>&lt;p&gt;Most of the articles which I found on the web about the &lt;code&gt;System.Text.Json&lt;/code&gt; API dealt with serialization and de-serialization with little being explored about creating a JSON entity on the fly. &lt;br&gt;
A recent use-case required us to build a data entity dynamically while running through a set of business logic (emphasis on the fact that the output here could not be modelled as a POCO since it was going to take form during the run-time). This entity would be serialized and sent over to a downstream service. While one could opt for the&lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/interop/walkthrough-creating-and-using-dynamic-objects" rel="noopener noreferrer"&gt; Dynamic Objects&lt;/a&gt;, we decided to work our way with JSON objects.&lt;/p&gt;

&lt;p&gt;When working with dynamic data through the &lt;code&gt;System.Text.Json&lt;/code&gt; API, everything revolves around the &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.nodes.jsonnode?view=net-6.0" rel="noopener noreferrer"&gt;JsonNode&lt;/a&gt; class. It is an abstract class and is derived by the &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.nodes.jsonarray?view=net-6.0" rel="noopener noreferrer"&gt;JsonArray&lt;/a&gt;, the &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.nodes.jsonobject?view=net-6.0" rel="noopener noreferrer"&gt;JsonObject &lt;/a&gt;and  the &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.nodes.jsonvalue?view=net-6.0" rel="noopener noreferrer"&gt;JsonValue&lt;/a&gt;. These can be thought of as the building blocks - we work with them more in the coming examples.&lt;/p&gt;

&lt;p&gt;Before we actually dive into the &lt;code&gt;System.Text.Json&lt;/code&gt; API, let's take a step back and re-visit the JSON standard. &lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.json.org/json-en.html" rel="noopener noreferrer"&gt;json.org&lt;/a&gt; quotes the following&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JSON is built on two structures:&lt;br&gt;
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.&lt;br&gt;
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It then elaborates how in JSON, this could take two forms - either an object and an array. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An object and an array in JSON can have a JSON value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A value in JSON can also in turn be an array, a number a object , etc&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We kind of get an idea that a json value, json object and the json array are all intertwined. &lt;/p&gt;

&lt;p&gt;Going back to System.Text.Json API, we have the &lt;code&gt;JsonObject&lt;/code&gt; which can be used to realise an object in JSON, &lt;code&gt;JsonArray&lt;/code&gt;as an array and &lt;code&gt;JsonValue&lt;/code&gt;for a value in JSON.&lt;/p&gt;

&lt;p&gt;We start with &lt;code&gt;JsonValue&lt;/code&gt; and build our way up. The &lt;code&gt;JsonValue&lt;/code&gt;is an abstract class (you cannot instantiate an abstract class). The next obvious thought is - how are we then going to realise a C# type here  without being able to instantiate the type ? Our C# counterpart could be anything string, double, int, etc.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;JsonValue&lt;/code&gt; class contains static methods (&lt;code&gt;Create&lt;/code&gt;) for the C# value types and based on in-built implicit conversion, they are going to give you a type of &lt;code&gt;JsonNode&lt;/code&gt; for the C# value you provided.&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;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Doofus"&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;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Flintstones"&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;addressPostcode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1234&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;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1234567890&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;knowsCSharp&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can always be explicit about the types too,&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;identifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NewGuid&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;yearAtDev&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might wonder what's the advantage here ? Also we said we want a  JsonValue but we are getting back a JsonNode - it will make sense in a while but for now what we can assert here is the type definition.&lt;/p&gt;

&lt;p&gt;As of .NET 8, through &lt;code&gt;System.Text.Json&lt;/code&gt; API we also get &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonvaluekind?view=net-8.0" rel="noopener noreferrer"&gt;JsonValueKind&lt;/a&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;enum&lt;/span&gt; &lt;span class="n"&gt;JsonValueKind&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;Undefined&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;Array&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;Number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;True&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;False&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="n"&gt;Null&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While working with serialization and deserialization of data, the widely adopted standard is JSON. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;JsonValue.Create&amp;lt;int&amp;gt;(5)&lt;/code&gt;  would create a JSON value of kind number and &lt;br&gt;
&lt;code&gt;JsonValue.Create&amp;lt;string&amp;gt;("5")&lt;/code&gt; would create a JSON value of kind string. This would matter when this data would be deserialized in any downstream system&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;would be deserialized differently, age would have to be deserialized as an integer and age2 as a string. Hence it is good to assert type definitions while building up the json data at the source and through the different factory method overloads (can also be achieved with the implicit conversions) , &lt;code&gt;System.Text.Json&lt;/code&gt; API provides a cleaner way&lt;/p&gt;

&lt;p&gt;We can also use the  &lt;code&gt;GetValueKind&lt;/code&gt; method to get the &lt;code&gt;JsonValueKind&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;guid&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NewGuid&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;jsonValueKind&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;guid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetValueKind&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have a fair understanding of the &lt;code&gt;JsonValue&lt;/code&gt;, we try to use that to realise a &lt;code&gt;JsonArray&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;JsonArray&lt;/code&gt; is but a collection of json values or json objects.&lt;/p&gt;

&lt;p&gt;To be completely honest, I haven't explored much with &lt;code&gt;JsonArray&lt;/code&gt;except for a deserialization use-case. However I skimmed through the documentation and here is one sample to create a JSON array&lt;br&gt;
&lt;code&gt;JsonArray&lt;/code&gt; is a sealed class, you cannot inherit it but you can instantiate it.&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;jsonArray&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;JsonArray&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="n"&gt;jsonArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;23&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="n"&gt;jsonArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"23"&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;Add&lt;/code&gt; method on the &lt;code&gt;JsonArray&lt;/code&gt; accepts a parameter of type &lt;code&gt;JsonNode&lt;/code&gt; and if you recall the definition from the &lt;em&gt;json.org&lt;/em&gt; , a Json array could consist of a JSON object or  a JSON value - this blends quiet well here, that a &lt;code&gt;JsonNode&lt;/code&gt;in &lt;code&gt;System.Text.Json&lt;/code&gt; could be very well be substituted by JsonValue or &lt;code&gt;JsonObject&lt;/code&gt; i.e. you can add either of the types through the &lt;code&gt;Add&lt;/code&gt;method on the &lt;code&gt;JsonArray&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;But please feel free to correct me here and add any relevant use case with &lt;code&gt;JsonArray&lt;/code&gt;if you know of :)&lt;/p&gt;

&lt;p&gt;Moving on to the &lt;code&gt;JsonObject&lt;/code&gt;, &lt;/p&gt;

&lt;p&gt;&lt;code&gt;JsonObject&lt;/code&gt; is basically a collection &lt;code&gt;KeyValuePair&lt;/code&gt; of string and &lt;code&gt;JsonNode&lt;/code&gt; ! It derives from the &lt;code&gt;JsonNode&lt;/code&gt; and well holds a &lt;code&gt;JsonNode&lt;/code&gt; too. &lt;/p&gt;

&lt;p&gt;In the beginning, the &lt;code&gt;JsonNode&lt;/code&gt; had got me all confused but if you take a moment and appreciate the tree data structure and think of JSON as one, then a tree is just a collection of nodes. With JSON, the different entities are a value, object and an array so a node could be any one of these ? and may be then it makes sense that the &lt;code&gt;JsonValue&lt;/code&gt;, &lt;code&gt;JsonArray&lt;/code&gt; and &lt;code&gt;JsonObject&lt;/code&gt; derive from the &lt;code&gt;JsonNode&lt;/code&gt; . Anyway, I like to look at it that way.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;JsonObject&lt;/code&gt; then its just putting together the json values, arrays or even nested objects&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;jsonObject&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Nodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;JsonObject&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="s"&gt;"firstName"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Doofus"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"lastName "&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Flintstones"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Nodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JsonObject&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"line1"&lt;/span&gt;&lt;span class="p"&gt;]=&lt;/span&gt; &lt;span class="s"&gt;"Stockholm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"line2"&lt;/span&gt;&lt;span class="p"&gt;]=&lt;/span&gt; &lt;span class="s"&gt;"Sweden"&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"knowsCSharp"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"yearsAtDev "&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"hobbies"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;JsonArray&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"music"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"dance"&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;Also now might be a good time to look at how we could work with deserialization here&lt;/p&gt;

&lt;p&gt;Usually when we get data over the wire, we get a JSON. In our consumer when we get a JSON response (say through a REST call) , we deserialize it in a POCO&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "FirstName": "Doofus",
  "LastName": "Flintstones",
  "Address": {
    "street": {
      "line1": "Stockholm",
      "line2": "Sweden",
      "line3": "Nordic"
    },
    "postcode": 1234
  }
}

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

&lt;/div&gt;



&lt;p&gt;the POCO could be modelled as&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;Person&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;FirstName&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&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;LastName&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;Address&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Address&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Street&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;Street&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;Postcode&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Street&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;Line1&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&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;Line2&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&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;Line3&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;and we get the deserialized data by doing -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var person = System.Text.Json.JsonSerializer.Deserialize&amp;lt;Person&amp;gt;(jsonResponseAsString);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But what if we instead deserialize into a JsonObject and not a POCO ?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var personAsJsonObject = System.Text.Json.JsonSerializer.Deserialize&amp;lt;JsonObject&amp;gt;(jsonResponseAsString);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you have used Newtonsoft before, you might be familiar with the JObject and we used an indexer to traverse it.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;JsonObject&lt;/code&gt; if you recall is a key value pair, the value being of type &lt;code&gt;JsonNode&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we want to access the property FirstName then we could either use the indexer&lt;/p&gt;

&lt;p&gt;&lt;code&gt;personAsJsonObject["firstName"]&lt;/code&gt; or &lt;code&gt;personAsJsonObject.TryGetPropertyValue("firstName", out var value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both are going to give you a &lt;code&gt;JsonNode&lt;/code&gt;, but a JsonNode could be either  a JsonValue,a JsonArray or JsonObject how would you know ?&lt;/p&gt;

&lt;p&gt;On JsonNode, we can call AsValue(), AsArray() and AsObject() to get the corresponding type. Next, we could also assert the kind through GetValueKind and call any one of the factory methods we saw earlier to retrieve our value.&lt;/p&gt;

&lt;p&gt;I did it very old-school way, something on the lines of this , you can have one each for JsonObject and JsonArray as well&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;static&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt; &lt;span class="nf"&gt;ToJsonValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;JsonNode&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AsValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;InvalidOperationException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//handle it at the caller level by letting it fall-back to &lt;/span&gt;
      &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;perhaps&lt;/span&gt; &lt;span class="n"&gt;JsonArray&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;Following is an extract from the .NET source code, and as you see an &lt;code&gt;InvalidOperationException&lt;/code&gt;is going to get thrown if you try to call &lt;code&gt;AsValue()&lt;/code&gt; -  say for example on a &lt;code&gt;JsonObject&lt;/code&gt;( i.e. &lt;code&gt;JsonNode&lt;/code&gt;which is of type &lt;code&gt;JsonObject&lt;/code&gt;) . &lt;code&gt;AsValue()&lt;/code&gt; would only yield a &lt;code&gt;JsonValue&lt;/code&gt;on being called on a &lt;code&gt;JsonNode&lt;/code&gt;which holds a &lt;code&gt;JsonValue&lt;/code&gt;, for others we get an exception&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="n"&gt;JsonValue&lt;/span&gt; &lt;span class="nf"&gt;AsValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;jValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;JsonValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jValue&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;ThrowHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ThrowInvalidOperationException_NodeWrongType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;JsonValue&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;jValue&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;Next after you have retrieved a JsonValue, you can call the nice helper methods to retrieve a string, boolean, int, etc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jsonValue.TryGetValue(out string? value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;While there is a lot of ground to cover here and also to learn, even for me ..I hope this serves as a starting point/ basics when you in future would want to explore this path&lt;/p&gt;

&lt;p&gt;Any suggestions/improvements welcome.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>programming</category>
    </item>
    <item>
      <title>== problem with generics</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Fri, 05 Jan 2024 19:37:14 +0000</pubDate>
      <link>https://dev.to/vparab/-problem-with-generics-44b8</link>
      <guid>https://dev.to/vparab/-problem-with-generics-44b8</guid>
      <description>&lt;p&gt;Evaluating equality for a type would mean to define the conditions any two instances of it, must meet in order for them to be considered equal. &lt;/p&gt;

&lt;p&gt;.NET by default, gives you reference equality out of the box - two different instances of a type would never evaluate to be equal because they would essentially point to different memory locations.&lt;/p&gt;

&lt;p&gt;Having said that, .NET also gives us the possibility to override the reference equality provided by default and define our definition of when two instances of a type be considered equal. These are made available through the &lt;code&gt;System.Object&lt;/code&gt; contract -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.object.equals?view=net-8.0" rel="noopener noreferrer"&gt;&lt;code&gt;virtual bool Equals(object? obj)&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;static bool Equals (object? objA, object? objB)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.object.referenceequals?view=net-8.0" rel="noopener noreferrer"&gt;&lt;code&gt;static bool ReferenceEquals (object? objA, object? objB)&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Few things to remember here is &lt;code&gt;ReferenceEquals&lt;/code&gt; is internally called by the default implementation of &lt;code&gt;Equals(object? obj)&lt;/code&gt; to give us the reference equality and also the &lt;code&gt;static bool Equals (object? objA, object? objB)&lt;/code&gt; internally calls the default virtual Equals. The static implementation is given as an additional sanity check if we ever try to evaluate a null instance for equality.&lt;/p&gt;

&lt;p&gt;So if we want to override the default equality, we override the above methods in our type. &lt;/p&gt;

&lt;p&gt;Also if we look closely, the argument for the Equals method is &lt;code&gt;object&lt;/code&gt; and all the types in .NET derive from object class, this would definitely come with an overhead of casting our type (for which we are defining equality). To make things efficient, .NET also gives us the IEquatable contract which ensures type safety.&lt;/p&gt;

&lt;p&gt;The last thing to remember before we move on to the actual purpose of the post, is the == operator we use to check if two instances are equal. Well this is not from .NET and it is a C# operator.&lt;/p&gt;

&lt;p&gt;If we define our own equality for one of our types by overriding the previously mentioned three methods and the  IEquatable contract and not override the == operator then we are bound to get ambiguous results. The C# operator == by default for a reference type will always evaluate the reference equality&lt;/p&gt;

&lt;p&gt;Consider the  &lt;em&gt;Person&lt;/em&gt; type here, lets assume that for two person instances to be equal we will just look at the &lt;em&gt;Name&lt;/em&gt; and not the City value. Two &lt;em&gt;Person&lt;/em&gt; instances with same  &lt;em&gt;Name&lt;/em&gt; value are equal. We define the equality here as follows&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;IEquatable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;&amp;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;Name&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&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;City&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

 &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="p"&gt;==(&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&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="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="p"&gt;!=(&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&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;bool&lt;/span&gt; &lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;other&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="n"&gt;Name&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;bool&lt;/span&gt; &lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;Person&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="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;obj&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="k"&gt;false&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="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;GetHashCode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetHashCode&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;Now consider the below snippet, if we were to write a generic method which checks if the two person instances are equal we probably would do something like this&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;person1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Doofus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;City&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"London"&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;person2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Doofus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;City&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Madrid"&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;$"Are they equal through Equals ? &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;IsThePersonSameCheckWithEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;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;$"Are they equal through Operator? &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;IsThePersonSameCheckWithOperator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;IsThePersonSameCheckWithEquals&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;
&lt;span class="err"&gt;{&lt;/span&gt;
 &lt;span class="nc"&gt;return&lt;/span&gt; &lt;span class="n"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;IsThePersonSameCheckWithOperator&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
&lt;span class="err"&gt;{&lt;/span&gt;
 &lt;span class="nc"&gt;return&lt;/span&gt; &lt;span class="n"&gt;person1&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person2&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;If we observe the outputs, the equality evaluates to false with == operator in spite of us overriding it in our type &lt;code&gt;Person&lt;/code&gt; .&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For Generics, the compiler assumes that there is no overload for the == operator (and also disregards the one we wrote ourselves) and checks for reference equality instead&lt;br&gt;
We should instead use the Equals to compare instances in a generic code implementation&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Basics of accessing the Configuration in .NET Web App</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Tue, 19 Sep 2023 15:41:35 +0000</pubDate>
      <link>https://dev.to/vparab/basics-of-accessing-the-configuration-in-net-web-app-242c</link>
      <guid>https://dev.to/vparab/basics-of-accessing-the-configuration-in-net-web-app-242c</guid>
      <description>&lt;p&gt;In the previous post we talked about Configuration Sources and Configuration Provider - two different concepts but intertwined together.&lt;/p&gt;

&lt;p&gt;We also spoke in brief about how this &lt;/p&gt;

&lt;p&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;wires up the defaults for our apps (in terms of logging, dependency injections, configuration, etc. )&lt;/p&gt;

&lt;p&gt;We also established that &lt;code&gt;appsettings.json&lt;/code&gt; and &lt;code&gt;appsettings.{environment}.json&lt;/code&gt; are amongst the default configuration sources and are loaded by default through the json configuration provider. In addition to the defaults, we can also add our own custom sources and of course they would need their own custom configuration providers defined (i.e. you need to write your &lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0#custom-configuration-provider" rel="noopener noreferrer"&gt;own&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Let's look at how you can access the configuration within your application from the &lt;code&gt;builder&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Say you have an appsettings.json file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ConnectionStrings"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SqliteConnection"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Filename=:memory"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PostgresConnection"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Post&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;gres&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;connection&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;goes&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;here"&lt;/span&gt;
    &lt;span class="pi"&gt;},&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logging"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LogLevel"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Default"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Information"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Microsoft.AspNetCore"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Warning"&lt;/span&gt;
        &lt;span class="pi"&gt;}&lt;/span&gt;
    &lt;span class="pi"&gt;},&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AllowedHosts"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Message"&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Roofus&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Doofus&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;!!"&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Title"&lt;/span&gt; &lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HomePage"&lt;/span&gt; &lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Stockholm&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;i&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;mitt&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;hjärta"&lt;/span&gt;
       &lt;span class="pi"&gt;}&lt;/span&gt;
&lt;span class="pi"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you want to access the &lt;strong&gt;&lt;em&gt;Message&lt;/em&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;em&gt;ConnectionStrings&lt;/em&gt;&lt;/strong&gt; in your application. Since appsettings.json is shipped by the .NET framework and loaded by default, we don't have to worry about that part.&lt;/p&gt;

&lt;p&gt;We can access the Message config from the file ,in its most basic form through an indexer on the &lt;code&gt;Configuration&lt;/code&gt; collection provided by the &lt;code&gt;builder&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var message = builder.Configuration["Message"];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The config which we want to access becomes the key here&lt;/p&gt;

&lt;p&gt;The connection string for &lt;em&gt;Sqlite&lt;/em&gt; can be retrieved as :&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;sqlConnectionString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetConnectionString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SqliteConnection"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you closely observe &lt;strong&gt;&lt;em&gt;SqliteConnection&lt;/em&gt;&lt;/strong&gt; is a level below (or nested within) &lt;strong&gt;&lt;em&gt;ConnectionStrings&lt;/em&gt;&lt;/strong&gt; . Usually, if we have to access a nested config then we do it through the "&lt;strong&gt;:&lt;/strong&gt;" notation. &lt;br&gt;
But the GetConnectionString is an extension method provided by the framework and it does the following behind the scenes&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;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetConnectionString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IConfiguration&lt;/span&gt; &lt;span class="n"&gt;configuration&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;GetSection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ConnectionStrings"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But since the DI is one of the key highlights of .NET framework, we can inject the configuration as a dependency which will be resolved by .NET framework itself&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Configuration&lt;/code&gt; on the &lt;code&gt;builder&lt;/code&gt; is of type &lt;code&gt;ConfigurationManager&lt;/code&gt; which is a concrete implementation of &lt;code&gt;IConfigurationBuilder&lt;/code&gt;, &lt;code&gt;IConfigurationRoot&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;IConfigurationRoot&lt;/code&gt; extends &lt;code&gt;IConfiguration&lt;/code&gt; and as msdn highlights &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration#binding" rel="noopener noreferrer"&gt;here&lt;/a&gt; , &lt;code&gt;IConfiguration&lt;/code&gt; represents a set of key/value application configuration properties and that is what we are interested in.&lt;/p&gt;

&lt;p&gt;So we inject a dependency of &lt;code&gt;IConfiguration&lt;/code&gt; in our services if we want to access any configuration within it&lt;/p&gt;

&lt;p&gt;Following is a snippet from a &lt;code&gt;RouteEndpoint&lt;/code&gt; (an endpoint handler) from the minimal api scaffold&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="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;MapGet&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="n"&gt;FromServices&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;IConfiguration&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sb&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;StringBuilder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

   &lt;span class="c1"&gt;//By default the appsettings.json is loaded by the json configuration provider when the host is built&lt;/span&gt;

   &lt;span class="c1"&gt;//if we have to access the configuration, we can do it via the IConfiguration interface&lt;/span&gt;

   &lt;span class="c1"&gt;//The hierarchical structure in the json file is flattened like this. you access the children through ":" notation&lt;/span&gt;

   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetValue&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"Title:HomePage"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Homepage Title : &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; \n"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//If you try to access a non-existent key, it will set the default value&lt;/span&gt;

   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;nonExistentKeyInConfiguration&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetValue&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"Title:Shopping"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Trying to access a non-existent key from the configuration  will set the default value of T on GetValue&amp;lt;T&amp;gt;: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nonExistentKeyInConfiguration&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; \n"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;span class="c1"&gt;//And if you try to cast the config to an invalid cast, e.g. our Title config is given as a string and we try to cast it to int then we will get an exception&lt;/span&gt;

   &lt;span class="k"&gt;try&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;inconsistentTypeCasting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetValue&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"Title:HomePage"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Inconsistent type casting of a config value while retrieving it will throw an exception of &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InnerException&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;Source&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;\n"&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;sb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&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;Lets look at a more complex config and how to map it to a csharp complex type&lt;br&gt;
You have the following in your &lt;code&gt;appsettings.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"Pet"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Roofus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"I climb through people's house fences and scare them'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"IsFriendly"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this you would create a complex type say Pet :&lt;br&gt;
(OBS - The type name can be different than the config name, it does not have to match)&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;Pet&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;Name&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&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;Description&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;IsFriendly&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;Age&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Going back to our json config,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"Pet"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Roofus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"I climb through people's house fences and scare them'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"IsFriendly"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"Age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"Pet" is referred as a &lt;strong&gt;&lt;em&gt;section&lt;/em&gt;&lt;/strong&gt; and we bind the "section" calling the Bind on the &lt;code&gt;IConfiguration&lt;/code&gt; injected in our service&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="c1"&gt;//we create the pet object where we want to bind the configuration&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;petUsingBind&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;Pet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

   &lt;span class="c1"&gt;//Call the bind with the section name and pass the object created.&lt;/span&gt;
   &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Pet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;petUsingBind&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;To wrap up , &lt;br&gt;
In the previous post we saw that the default configurations are loaded in a particular order and the order in which the configuration sources are loaded determines if a config overrides the other i.e. the order matters !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmvbgsw2jvv82xv2o36vf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmvbgsw2jvv82xv2o36vf.jpg" alt="Config Sources in Order" width="444" height="575"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, the good news is we can override the default behaviour and define our own through the &lt;code&gt;ConfigureAppConfiguration&lt;/code&gt;available on the &lt;code&gt;host&lt;/code&gt;exposed through &lt;code&gt;builder&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ConfigureAppConfiguration&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;configBuilder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;//we clear all the config sources previously read in the default order &lt;/span&gt;

 &lt;span class="n"&gt;configBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&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;env&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HostingEnvironment&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

 &lt;span class="n"&gt;configBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddEnvironmentVariables&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ASPNETCORE_"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="c1"&gt;//we are only reading the appsettings.json file here. explicitly skipping the environment specific&lt;/span&gt;
 &lt;span class="n"&gt;configBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddJsonFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"appsettings.json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;reloadOnChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//We skip the user secrets&lt;/span&gt;
&lt;span class="c1"&gt;//We skip the command line arguments as well&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We will look at the options pattern in the next post&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The msdn documentation on&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0" rel="noopener noreferrer"&gt; fundamentals of configuration&lt;/a&gt; is really good and has all you need to know information. Please check it out to explore more in this area&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.jondjones.com/programming/aspnet-core/how-to/essential-net-6-app-settings-tips-master-developer-and-environment-specific-settings/" rel="noopener noreferrer"&gt;Customization through &lt;code&gt;ConfigureAppConfiguration&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further &lt;a href="https://andrewlock.net/exploring-dotnet-6-part-1-looking-inside-configurationmanager-in-dotnet-6/" rel="noopener noreferrer"&gt;reading &lt;/a&gt; on ConfigurationManager (type of builder.Configuration)&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>beginners</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Configuration Source vs Configuration Provider</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Thu, 14 Sep 2023 09:31:53 +0000</pubDate>
      <link>https://dev.to/vparab/configuration-source-vs-configuration-provider-47e3</link>
      <guid>https://dev.to/vparab/configuration-source-vs-configuration-provider-47e3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is part 1 of the series I intend to write about getting onboarded with configuration in ASPNETCORE. I will try to post the follow up articles soon&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we actually talk about configuration in ASPNETCORE, it is important to address the Configuration Source and the Configuration Provider. We try to do that here&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration Source
&lt;/h2&gt;

&lt;p&gt;Configuration Source is the one which will act as the "input" to configure our application. It could be a json file, command line argument, an environment variable, the dotnet user secrets or it could even be an external config store - &lt;a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html" rel="noopener noreferrer"&gt;AWS Parameter Store&lt;/a&gt; or even a SQL database. &lt;/p&gt;

&lt;p&gt;We all are familiar with the &lt;code&gt;appsettings.json&lt;/code&gt; which gets shipped by default when we ask for a ASPNET Core web application scaffold. This then is a &lt;em&gt;configuration source&lt;/em&gt;, where you would define the configuration to be used within your application as key value pairs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The only important thing to stress here is that the configuration source is anything where we specify any "&lt;em&gt;settings/configurations&lt;/em&gt;" with their "&lt;em&gt;values&lt;/em&gt;" which we can feed in as a "&lt;em&gt;configuration&lt;/em&gt;" to our application&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Configuration Provider
&lt;/h2&gt;

&lt;p&gt;Having gotten the configuration source out of our way now, its quiet easy to understand the concept of a configuration provider.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CreateBuilder&lt;/code&gt; is kind of responsible to configure logging, set up the dependency injection container, configure a default host and also make available the default configuration sources to our application. The &lt;code&gt;CreateBuilder&lt;/code&gt;is what makes us available the default "&lt;strong&gt;host&lt;/strong&gt;". Using the default host object, we can also add more &lt;em&gt;configuration sources&lt;/em&gt; to it using the extensions provided by the ASPNETCORE framework.&lt;/p&gt;

&lt;p&gt;Well, the first thing you would do is to define a configuration source (lets say it is a json file - you have all your configurations and their values in it) but then you must make the ASPNETCORE know of it in order for it to be available in your application.&lt;/p&gt;

&lt;p&gt;Comes in the &lt;em&gt;Configuration Provider&lt;/em&gt; . It will "load" a specific configuration source and then make it available in your application, in its most basic form through &lt;code&gt;builder.Configuration&lt;/code&gt; (more on it later)&lt;/p&gt;

&lt;p&gt;Some of the default configuration providers by ASPNETCORE are Command-Line Configuration Provider, Environment variables configuration provider, JSON configuration provider. When we say default, it means ASPNETCORE will automatically handle them for us we need not do anything.&lt;/p&gt;

&lt;p&gt;Following is a snippet from &lt;a href="https://github.com/dotnet/runtime/blob/6149ca07d2202c2d0d518e10568c0d0dd3473576/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L229-L256" rel="noopener noreferrer"&gt;here&lt;/a&gt; . This is what is happening behind the scenes when ASPNETCORE is wiring up a default builder for you . Also notice, the order in which it loads the default configurations sources through their respective providers . So in the most vanilla form, any configuration provided by the command line arguments would override the ones before i.e. if you have a config key &lt;code&gt;isKanelbulle&lt;/code&gt; set as &lt;code&gt;false&lt;/code&gt; in your &lt;code&gt;appsetting.json&lt;/code&gt; and while running the application, you pass in the same as an command line argument &lt;code&gt;isKanelbulle&lt;/code&gt; set to &lt;code&gt;true&lt;/code&gt;, the &lt;strong&gt;true&lt;/strong&gt; overrides the &lt;strong&gt;false&lt;/strong&gt; value set in the json file&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="n"&gt;appConfigBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddJsonFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"appsettings.json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reloadOnChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;reloadOnChange&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddJsonFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"appsettings.&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EnvironmentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reloadOnChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;reloadOnChange&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsDevelopment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ApplicationName&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;try&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;appAssembly&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Assembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Load&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;AssemblyName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ApplicationName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                    &lt;span class="n"&gt;appConfigBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddUserSecrets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;appAssembly&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reloadOnChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;reloadOnChange&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FileNotFoundException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="c1"&gt;// The assembly cannot be found, so just skip it.&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;appConfigBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddEnvironmentVariables&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="nf"&gt;AddCommandLineConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;appConfigBuilder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Putting it together now,&lt;/p&gt;

&lt;p&gt;Say you have a json file as the configuration source - lets call it &lt;code&gt;MyDefaultConfigurations.json&lt;/code&gt; . Then to make it available in the application, you would instruct the default json configuration provider of ASPNETCORE to "load" it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;builder.Configuration.AddJsonFile("MyDefaultConfigurations.json",&lt;br&gt;
optional: true,reloadOnChange: true);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;OBS - &lt;code&gt;appsettings.json&lt;/code&gt; and &lt;code&gt;appsettings.{Environment}.json&lt;/code&gt; are loaded by default, you don't have to register them explicitly as done above&lt;/p&gt;

&lt;p&gt;Also an important thing to consider here is - in addition to the default configuration providers, you can build your own custom configuration providers as well. &lt;/p&gt;

&lt;p&gt;A use case of this would be - say you have an external API which is responsible to give you some configurations after running a complex logic. &lt;/p&gt;

&lt;p&gt;This then would become your configuration source. ASPNETCORE would obviously be unaware of this and none of the default providers would know how to handle it. This is then a good candidate for you to write your custom config provider which say would make a REST call to this service of yours to get the configuration and make it available in your application. There is already a good documentation on how to implement your custom configuration provider, link below.&lt;/p&gt;

&lt;p&gt;Please feel free to correct :)&lt;/p&gt;

&lt;p&gt;References&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0#default-application-configuration-sources" rel="noopener noreferrer"&gt;default-application-configuration-sources&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0#custom-configuration-provider" rel="noopener noreferrer"&gt;Custom Configuration Provider Example&lt;/a&gt;&lt;/p&gt;




</description>
      <category>dotnet</category>
      <category>aspnetcore</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>AutoFixture - Specimen Builders</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Tue, 12 Sep 2023 21:44:00 +0000</pubDate>
      <link>https://dev.to/vparab/autofixture-specimen-builders-135k</link>
      <guid>https://dev.to/vparab/autofixture-specimen-builders-135k</guid>
      <description>&lt;p&gt;I will try to keep this one short by not going into details of what &lt;a href="https://github.com/AutoFixture/AutoFixture#overview" rel="noopener noreferrer"&gt;Autofixture&lt;/a&gt; is , why we need it... I am assuming you would be familiar with it&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a specimen ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In programming we work with types(i.e. &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;decimal&lt;/code&gt;, etc. ) -  &lt;em&gt;&lt;strong&gt;a specimen is an example value of a particular type&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;E.g. "Kanelbulle" is a specimen (an example value) for the type string or 0.23m , 0.50m are specimens for the type decimal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a specimen builder in context Auto fixture ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Having known what a specimen is now, we can safely assume that the logic ( basically set of instructions) to build a specific specimen is a specimen builder. &lt;/p&gt;

&lt;p&gt;In terms of AutoFixture, it is a concrete implementation of the &lt;a href="https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixture/Kernel/ISpecimenBuilder.cs" rel="noopener noreferrer"&gt;ISpecimenBuilder&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given a complex type like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee
{
 public string Name { get; set; }
 public int Code{ get; set; }
}

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

&lt;/div&gt;



&lt;p&gt;when we ask the fixture (&lt;code&gt;fixture.Create&amp;lt;Employee&amp;gt;()&lt;/code&gt;) to give us anonymous data of type Employee, behind the scenes a set of specimen builders (either default or custom) are going to run and generate us a specimen for a string and int (&lt;em&gt;which are basically the two properties of the Employee class here&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;For e.g. the default specimen builder for a string spits out a guid as the value for us. For a complex type , like Employee in this case, a string gets a specimen prepended with the property name (as it is the case for all the properties of a complex type) something like this &lt;em&gt;Namead6fb2f0-d23a-4bb0-a083-8ffca9a139aa&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before moving to writing a custom (concrete) implementation of the &lt;code&gt;ISpecimenBuilder&lt;/code&gt;, I want to put across the way I have understood the AutoFixture pipeline (feel free to correct me here)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AutoFixture is responsible for creating Anonymous data for a particular type of &lt;code&gt;T&lt;/code&gt; (** &lt;em&gt;anonymous data is anything which is not going to affect our SUT /system under test in anyway i.e. we don't care for the values but the instance or type is needed by our SUT regardless of its value&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;T could be a primitive type or any custom defined type&lt;/li&gt;
&lt;li&gt;When creating an instance (anonymous data) of &lt;code&gt;T&lt;/code&gt;, the first step is the customization phase where the AutoFixture library looks for a collection of the ISpecimenBuilders&lt;/li&gt;
&lt;li&gt;AutoFixture has its own set of default Specimen Builders and then if we write any custom ones,they will override those default specimen builders if registered in this phase
5.The customization phase is taken care by calling the &lt;code&gt;Customizations&lt;/code&gt; or the &lt;code&gt;Customize&lt;/code&gt; on the &lt;code&gt;fixture&lt;/code&gt; (we will see in a bit) instance&lt;/li&gt;
&lt;li&gt;The next step in the pipeline is then to "run" these concrete specimen builders to generate the specimen (example values) for a given type&lt;/li&gt;
&lt;li&gt;Last is the fall back step, where the Autofixture library applies fallbacks/defaults (tries real hard to generate a specimen for us !) and if still no specimen was able to be generated for a particular type(could be because of some validations in the constructor in case of a complex type), then eventually throw an error .&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;In nut shell , a request for fixture.Create() would go through a series of specimen builders to finally give us an instance of T&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;When would you want to create a custom specimen builder ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to influence the logic of building one in any specific way !&lt;/p&gt;

&lt;p&gt;For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var fixture = new Fixture();

var date1 = fixture.Create&amp;lt;DateTime&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default specimen builder for &lt;code&gt;DateTime&lt;/code&gt;shipped with Autofixture is going to return you a specimen (value) of a random date time value per your system's culture but say you want to override this and instead want the UTC date to be returned as a specimen&lt;/p&gt;

&lt;p&gt;In this case you can have your own specimen builder implementation. Refer &lt;a href="https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixture/CurrentDateTimeGenerator.cs" rel="noopener noreferrer"&gt;this&lt;/a&gt; and you can build your UTC one in a similar way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class CurrentUtcDateTimeGenerator : ISpecimenBuilder
    {

        public object Create(object request, ISpecimenContext context)
        {
            if (!typeof(DateTime).Equals(request))
            {
                return new NoSpecimen();
            }

            return DateTime.UtcNow;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then register this specimen builder (remember the Customize stage which we spoke about earlier ?) &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fixture.Customizations.Add(new CurrentUtcDateTimeGenerator());&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;next whenever we ask for an instance of type DateTime from this fixture it is going to give us the current UTC date&lt;/p&gt;

&lt;p&gt;A lot more things can be achieved through writing your own specimen builders.&lt;br&gt;
For e.g. say you have a class&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public class Employee&lt;br&gt;
 {&lt;br&gt;
  public string Name { get; set; }&lt;br&gt;
  public string EmployeeCode { get; set; }&lt;br&gt;
 }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You want to influence how EmployeeCode is generated , then you could override the default guid specimen for a string here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class EmployeeCodeGenerator : ISpecimenBuilder
 {

  private readonly char[] _allowedChars = { 'V', 'I', 'D', 'S', 'H', 'A' };

  public object Create(object request, ISpecimenContext context)
  {

   //we use reflection here and pattern matching to see if the specimen we are trying to build is a property
   //we are interested in the EmployeeCode property of the Employee class

   if (request is PropertyInfo propertyInfo)
   {

    //next we match the name to influence the specimen building logic for the EmployeeCode property

    if (propertyInfo.Name.Contains("employeecode", StringComparison.InvariantCultureIgnoreCase))
    {
     //when we get hold of it we return our custom 3 digit code instead

     return new string(_allowedChars, 0, 3);

    }

   }

   return new NoSpecimen();
  }
 }

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

&lt;/div&gt;



&lt;p&gt;and then use the above like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var fixture = new Fixture();
fixture.Customizations.Add(new EmployeeCodeGenerator());

var employee = fixture.Create&amp;lt;Employee&amp;gt;();
Assert.Equal(3, employee.EmployeeCode.Length);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this was useful&lt;/p&gt;

&lt;p&gt;Reference&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.ploeh.dk/2010/10/19/Convention-basedCustomizationswithAutoFixture/" rel="noopener noreferrer"&gt;Convention-based Customizations with AutoFixture&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>testing</category>
      <category>unittest</category>
    </item>
    <item>
      <title>Athena - Question regarding 'Create a table' options</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Sun, 20 Jun 2021 10:25:00 +0000</pubDate>
      <link>https://dev.to/vparab/athena-question-regarding-create-a-table-options-569k</link>
      <guid>https://dev.to/vparab/athena-question-regarding-create-a-table-options-569k</guid>
      <description>&lt;p&gt;I have just started learning AWS Athena through an online course and have been following it up with the official aws documentation. I understand that Athena uses the Hive as an underlying mechanism to issue ddl queries, so it is basically the Hive syntax which we use.&lt;/p&gt;

&lt;p&gt;From their documentation, I have trouble understanding these things :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; [COMMENT table_comment]
Creates the comment table property and populates it with the table_comment you specify.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is the comment table property and how does it helps us ? do we have any specific usage of this&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; [STORED AS file_format]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Specifies the file format for table data . &lt;em&gt;By specifying this we would instruct how the data obtained from querying a resource should be saved in the output s3 file&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But for this option, specifically &lt;br&gt;
INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname&lt;/p&gt;

&lt;p&gt;from where would we get the input_format_classname and output_format_classname ?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/athena/latest/ug/create-table.html" rel="noopener noreferrer"&gt;reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any leads to an alternative reference or documentation would be appreciated :)&lt;/p&gt;

</description>
      <category>help</category>
    </item>
    <item>
      <title>things I learnt today - delete remote branch through cli</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Thu, 03 Sep 2020 12:31:07 +0000</pubDate>
      <link>https://dev.to/vparab/things-i-learnt-today-37pg</link>
      <guid>https://dev.to/vparab/things-i-learnt-today-37pg</guid>
      <description>&lt;p&gt;list only the remote branches&lt;br&gt;
&lt;code&gt;git branch -r&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;delete a remote branch&lt;br&gt;
&lt;code&gt;git push -d origin &amp;lt;branch_name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;remove stale/obsolete remote branch references from local&lt;br&gt;
&lt;code&gt;git fetch --all --prune&lt;/code&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Can someone #explainlikeimfive the concept of virtual private cloud ?</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Wed, 12 Aug 2020 20:46:13 +0000</pubDate>
      <link>https://dev.to/vparab/can-someone-explainlikeimfive-the-concept-of-virtual-private-cloud-fk6</link>
      <guid>https://dev.to/vparab/can-someone-explainlikeimfive-the-concept-of-virtual-private-cloud-fk6</guid>
      <description>&lt;p&gt;Hello ! &lt;br&gt;
I have just started with my cloud journey and so far have a fair understanding of what public,private and hybrid clouds are.However finding it a bit difficult to wrap my head around VPC. Any references to post/links would be appreciated as well !&lt;/p&gt;

&lt;p&gt;Thanks !&lt;/p&gt;

</description>
      <category>explainlikeimfive</category>
    </item>
    <item>
      <title>c# basics -encapsulation , properties- manual,auto-implemented, readonly ,static</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Thu, 06 Aug 2020 14:46:14 +0000</pubDate>
      <link>https://dev.to/vparab/c-basics-encapsulation-properties-manual-auto-implemented-readonly-static-12mg</link>
      <guid>https://dev.to/vparab/c-basics-encapsulation-properties-manual-auto-implemented-readonly-static-12mg</guid>
      <description>&lt;p&gt;Object oriented programming (&lt;strong&gt;OOP&lt;/strong&gt;) focuses on modelling a business problem through objects. These objects are instances of a "Class". A &lt;strong&gt;class&lt;/strong&gt; thus serves as a blueprint to create objects from. A class apart from being a piece of code, carries data and behavior the object must posses.The data fields are referred to as &lt;strong&gt;properties&lt;/strong&gt; and the behavior are called as &lt;strong&gt;methods&lt;/strong&gt; , together they are referred as &lt;strong&gt;class members&lt;/strong&gt;. An important point to focus on here is one of the pillars of the OOP -Encapsulation. To encapsulate means to confine or encase something. In OOP we encapsulate a class by hiding its members. Here it is obvious to think, if we want to keep members hidden then why create the class in the first place ? However, when we create a class we have the liberty to expose the members. We decide which members should be accessible to the outside world , in view of this a class is also called as a "Contract". We are familiar with the access modifiers like private ,protected and public, which we decorate our class methods with. What about the data fields or the class properties ? Well, we expose them through &lt;em&gt;getters&lt;/em&gt; and &lt;em&gt;setters&lt;/em&gt;.Consider a class Employee, if we were to define a data field which holds an employee's name then the most basic way to do it manually as follows&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcyudltu3nmu9iqqy7dvq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcyudltu3nmu9iqqy7dvq.png" alt="Alt Text" width="708" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In here we are exposing the field &lt;em&gt;_employeeName&lt;/em&gt; through the &lt;em&gt;public&lt;/em&gt; property &lt;em&gt;EmployeeName&lt;/em&gt; in Employee (class) contract . We see two pieces of information,&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;get { }; set { };&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
. And mind you these are the most powerful tools we could use for our benefit. Since we are not directly exposing our data field &lt;em&gt;_employeeName&lt;/em&gt; , we have a great level of control to validate the value we set for this at a field level ! For e.g. we could write much complex logic in the &lt;em&gt;set { }&lt;/em&gt; block. We will get to it soon.&lt;br&gt;
Another way to implement properties is to use the "autoimplemented" version provided by visual studio (when you use the snippet- prop. type prop and press tab).We use &lt;em&gt;auto-implemented&lt;/em&gt; properties when we don't have to write any logic for getting or setting the data field - no validations or logic to get in the way- just plain simple accessing and setting the property. However an important thing to note here, behind the scenes the compiler creates a &lt;em&gt;backing field&lt;/em&gt; for them as a default. Same can be seen for the&lt;br&gt;
&lt;br&gt;
  &lt;code&gt;public int TimeHours { get; set; }&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
. Below is the IL (Intermediate Language) code for TimeHours&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhpgcjzk2kmut835ri62s.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhpgcjzk2kmut835ri62s.JPG" alt="Alt Text" width="800" height="544"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We could also make our properties &lt;em&gt;read-only&lt;/em&gt; , the only thing we need to do is to have a &lt;em&gt;private set;&lt;/em&gt; &lt;br&gt;
Say you have to calculate the pay for an employee. This salary depends on the hours the employee puts in and their designation. We don't want any external agent to set this value and our class is solely responsible to do this internally . We can achieve that through the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;_employeeName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;EmployeeName&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;get&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;_employeeName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="kd"&gt;set&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;_employeeName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;TimeHours&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;Designation&lt;/span&gt; &lt;span class="nx"&gt;EmployeeDesignation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;double&lt;/span&gt; &lt;span class="nx"&gt;EmployeePay&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;CalculateEmployeePay&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="kd"&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="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;CalculateEmployeePay&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;EmployeeDesignation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;JuniorDeveloper&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TimeHours&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MidLevelDeveloper&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TimeHours&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SeniorDeveloper&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TimeHours&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;Designation&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;JuniorDeveloper&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;MidLevelDeveloper&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;SeniorDeveloper&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we have made the "setter" of property &lt;em&gt;EmployeePay&lt;/em&gt; private thus making it &lt;em&gt;read-only&lt;/em&gt;. We also introduced a logic in here by calling the method &lt;em&gt;CalculateEmployeePay&lt;/em&gt; in the "getter" to return the employee's salary.&lt;/p&gt;

&lt;p&gt;So far what we have discussed are "instance properties". They are specific to the object instance and are not shared between instances. If you recall, we said the class is a blueprint to create objects.These are also known as instances.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;            &lt;span class="nx"&gt;Employee&lt;/span&gt; &lt;span class="nx"&gt;emp1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nx"&gt;emp1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;EmployeeName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Saania&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;emp1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;EmployeeDesignation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MidLevelDeveloper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;emp1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;TimeHours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="nx"&gt;Employee&lt;/span&gt; &lt;span class="nx"&gt;emp2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nx"&gt;emp2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;EmployeeName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hormaz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;emp2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;EmployeeDesignation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;JuniorDeveloper&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;emp2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;TimeHours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet emp1 and emp2 are two separate instances of the Employee class with each having their own set of class members (properties+methods). If I make change to the EmployeeName property of the instance &lt;em&gt;emp1&lt;/em&gt; then that is local to &lt;em&gt;emp1&lt;/em&gt; and would not reflect in &lt;em&gt;emp2&lt;/em&gt;. But what if there is a requirement that the Employee class holds a count for the total employees. Now this data would be global to an organization, we cannot have local copies for it. Say emp1 is our first employee and after instantiating it we do something like&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;emp1.TotalEmployeeCount += 1&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 then this change will be only confined to emp1 .When we create &lt;em&gt;emp2&lt;/em&gt; a fresh instance would be created setting all its data-members to their defaults (effectively if we had a property TotalEmployeeCount then for emp2 its value would be 0 at the beginning) i.e. the effect of emp1 is totally lost. In such scenarios we opt for &lt;strong&gt;static members&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Static properties&lt;/strong&gt; are common between instances. Any change made to them is global. Also one thing to note is , the static properties cannot be accessed through the instances unlike the rest where we use the "." (dot) operator e.g&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;emp2.EmployeeName = "Hormaz";&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 . They are accessed through the class name. Let us create a static property which will help us maintain a count for all the employees.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public static int TotalEmployees { get; set; }&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 . If we have to access them  then we will do that as&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;Employee.TotalEmployees += 1;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;It is also important to note that static data is not thread safe. Say this Employee class is a part of a larger distributed systems network. There are systems which modify the TotalEmployees count.It may happen so that two different systems try to modify the data at the same time. An obvious question would come to our mind that it is an atomic operation, we are just incrementing its count by one. We will go back to the autoimplemented properties concept. We saw that it is just a syntactic sugar for the manual property with a backing field. The compiler does  in fact creates a backing field for these properties. If we were to modify a property in essence it would be&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;read the value through "getter"&lt;/li&gt;
&lt;li&gt;increment the value by 1&lt;/li&gt;
&lt;li&gt;set the value through "setter"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It no longer is an atomic operation. There is a vulnerability that another thread (another process) gets access to its value before the previous one could increment and set the updated value. This leads to garbage data.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Format Enums as a Custom Name-Value Object List</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Sun, 26 Jul 2020 16:30:41 +0000</pubDate>
      <link>https://dev.to/vparab/format-enums-as-a-custom-name-value-object-list-250m</link>
      <guid>https://dev.to/vparab/format-enums-as-a-custom-name-value-object-list-250m</guid>
      <description>&lt;p&gt;I recently came across a scenario where I had to expose an enum property as a list of drop-down items to choose from. Typically when a user would select something from the drop-down and the form posted back to the server, the selection would be bound as a legit enum value.However the enum which we had defined was very system specific and not user savy.I could not just let the user select a particular enum value without giving out a description of the purpose it served.&lt;/p&gt;

&lt;p&gt;There is little we can do when defining enums as they have to follow certain conventions and sometimes we are required to give them system specific values which might not be very descriptive to the end user. E.g. SYSTEM_ERROR_102 which might be some error only known to the developer.We cannot show this directly to the user and have to give some description about it.&lt;/p&gt;

&lt;p&gt;In the example ahead, let us assume we have a simple form with two inputs - a textbox and a drop-down. The user has to enter a product name and assign it with a product type (select from the drop-down). At the back-end we are maintaining a Product enum as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Board&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Desk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Carpet&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For demo purpose let us assume the form is in Swedish and we need to show the product types in Swedish but at the same time when user selects a particular product type and the form is posted back it must be bound to one of the Product enum values . (Note the following is just one way to achieve it, there can be different ways to do it. Also your use case could be different, I have assumed this example for simplicity but the technique remains the same)&lt;/p&gt;

&lt;p&gt;We start by defining our Product Enum as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bord&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
  &lt;span class="nx"&gt;Board&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Stol&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
  &lt;span class="nx"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SkrivBord&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
  &lt;span class="nx"&gt;Desk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Matta&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
  &lt;span class="nx"&gt;Carpet&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have made use of the DescriptionAttribute from the System.ComponentModel namespace.You can also go for your own custom attribute but since this is readily available we are good to go here. You can read about the Description Attribute &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.descriptionattribute?view=netcore-3.1" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
This Description Attribute will have whatever friendly name you want your Enum value to be associated with.&lt;/p&gt;

&lt;p&gt;My back-end was an API and I wanted to return a list of anonymous object of type Name and Value. The name would be what shown as a selection in the list and the value would be bound to the selection. In our example here, the Name would be the Swedish counterpart and the value would be the actual enum value (Table,Desk,etc).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;dynamic&lt;/span&gt; &lt;span class="nc"&gt;FormatEnum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Enum&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;

 &lt;span class="cm"&gt;/*Since any valid (derived) Enum type could be passed to this function
 we need the exact Enum type passed to this func
 which we would require to process further.Here it would be "Product".
 */&lt;/span&gt;
 &lt;span class="c1"&gt;//type=Product;&lt;/span&gt;
 &lt;span class="nx"&gt;Type&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="cm"&gt;/*
Enums hold an integer value with the index starting from 0.
If we were to assign Product.Table and try to serialize it 
or save it in a db it would get stored as 1 
(Board=0, Table=1,Desk=2,Carpet=3).
Since we are working with dropdowns and want an user friendly interface, we are interested in the Name here.
Hence Enum.GetName(typeof(Product),Product.Table).
You could also do it with its index =&amp;gt; Enum.GetName(typeof(Product),1)
 */&lt;/span&gt;
 &lt;span class="c1"&gt;//name = Table&lt;/span&gt;
 &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Enum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;//Now we are interested in the Description Attribute.&lt;/span&gt;
 &lt;span class="cm"&gt;/*The FieldInfo class belongs to the System.Reflection namespace and as
defined in MSDN, "Discovers the attributes of a field and provides access to field metadata."*/&lt;/span&gt;
    &lt;span class="nx"&gt;FieldInfo&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//Out of all the properties listed by the FieldInfo object we are interested in the  CustomAttributes which would give us access to the DescriptionAttribute&lt;/span&gt;
     &lt;span class="nx"&gt;DescriptionAttribute&lt;/span&gt; &lt;span class="nx"&gt;descrp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DescriptionAttribute&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;Attribute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetCustomAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DescriptionAttribute&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
     &lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;descrp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;descrp&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="kc"&gt;null&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="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nx"&gt;descrp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Description&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&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 FieldInfo object would look like this and as shown above we are interested in the &lt;code&gt;CustomAttributes&lt;/code&gt; property &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs4nnj86j7t8tfjtv1hw9.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fs4nnj86j7t8tfjtv1hw9.JPG" alt="Alt Text" width="574" height="772"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we retrieve the DescriptionAttribute on an enum value we are interested in its "Description" property as that is what we set to begin with.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3upvn7fbdyrwtp41c3v0.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3upvn7fbdyrwtp41c3v0.JPG" alt="Alt Text" width="346" height="106"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And then you could use this method like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Enum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;Cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;FormatEnum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nc"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;Output :&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgvvam1sthqzzm12z0v1v.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgvvam1sthqzzm12z0v1v.JPG" alt="Alt Text" width="204" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Abstract or Virtual ?</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Thu, 23 Apr 2020 15:58:25 +0000</pubDate>
      <link>https://dev.to/vparab/abstract-or-virtual-19bj</link>
      <guid>https://dev.to/vparab/abstract-or-virtual-19bj</guid>
      <description>&lt;p&gt;We have come across abstract and virtual methods while programming and I often get startled when I see these two keywords. Whenever I try to look up for them a lot of "tabular comparisons" are being thrown at me, distinguishing between the two without highlighting why they are so ? &lt;/p&gt;

&lt;p&gt;I try to put together my understanding below. &lt;/p&gt;

&lt;p&gt;Abstract and virtual methods are a part of a concept called as &lt;strong&gt;&lt;em&gt;polymorphism&lt;/em&gt;&lt;/strong&gt; from object oriented programming. &lt;strong&gt;&lt;em&gt;Polymorphism&lt;/em&gt;&lt;/strong&gt; is further derived from the idea of &lt;strong&gt;&lt;em&gt;Inheritance&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
Why do we need Inheritance ? Inheritance is supposed to be the best feature of &lt;strong&gt;&lt;em&gt;OOP&lt;/em&gt;&lt;/strong&gt;. We can extend and reuse the capabilities of a class further as per the business needs i.e. writing less code ! How does &lt;strong&gt;&lt;em&gt;polymorphism&lt;/em&gt;&lt;/strong&gt; fits into this ? Say we have a class which outlines how a pizza should be . It also has a method called Prepare, data properties for the pizza name and price. In its most basic form, this method would output a plain pizza.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pizza&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;Price&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Prepare&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Here is a pizza dough base with some tomato sauce spread on it !&lt;/span&gt;&lt;span class="dl"&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;Someone walks up to us and tells us that now we also have to prepare Hawaiian and Pepperoni pizza. One option would be to make separate implementations for them.But we already have a Pizza class, which has a method &lt;em&gt;Prepare&lt;/em&gt; that outputs pizza in the basic form and also it has data properties which will help us to store the name and price, why not inherit from it (&lt;em&gt;remember -lesser code&lt;/em&gt;)? But won't the method &lt;em&gt;Prepare&lt;/em&gt; give us a basic pizza ? Override it ! &lt;/p&gt;

&lt;p&gt;We can override the method &lt;em&gt;Prepare&lt;/em&gt; to have two different behaviors - one which would give us the Hawaiian pizza and the other which would give us the  Pepperoni pizza. This right here is &lt;strong&gt;&lt;em&gt;polymorphism&lt;/em&gt;&lt;/strong&gt; - The &lt;em&gt;Prepare&lt;/em&gt; method behaving differently in different contexts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HawaiianPizza&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Pizza&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/*the field like name and price need not be written here as we inherit 
          them impiclitly from the parent Pizza class*/&lt;/span&gt;

       &lt;span class="c1"&gt;//overridden implementation of Prepare method to give us a hawaiian pizza&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PepperoniPizza&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Pizza&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="cm"&gt;/*the field like name and price need not be written here as we inherit 
          them impiclitly from the parent Pizza class*/&lt;/span&gt;

       &lt;span class="c1"&gt;//overridden implementation of Prepare method to give us a Pepperoni  pizza&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we established what &lt;strong&gt;&lt;em&gt;polymorphism&lt;/em&gt;&lt;/strong&gt; and inheritance is, it is equally important to know how to achieve those. &lt;strong&gt;&lt;em&gt;Inheritance&lt;/em&gt;&lt;/strong&gt; is when a class derives from another class or an interface.The class which other classes &lt;em&gt;derive&lt;/em&gt; from is called as a &lt;strong&gt;&lt;em&gt;base class&lt;/em&gt;&lt;/strong&gt;. Now this &lt;strong&gt;&lt;em&gt;base class&lt;/em&gt;&lt;/strong&gt; can be either an abstract class or a concrete class. Whenever a class is &lt;strong&gt;&lt;em&gt;incomplete&lt;/em&gt;&lt;/strong&gt; with at least one member not implemented then it is called as an &lt;strong&gt;&lt;em&gt;abstract&lt;/em&gt;&lt;/strong&gt; class. For e.g. the class Pizza below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pizza&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;Price&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Prepare&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;Here you can see that the method Prepare has no body or implementation (don't fret over the keyword abstract in that method, we will come to it soon !) . Given that an abstract class is incomplete , there is no point in creating any instance of it . What if an instance is created and someone calls the Prepare method ? &lt;br&gt;
Bam ! It would be a disaster since we lack its implementation. And there we have it - &lt;strong&gt;&lt;em&gt;Abstract classes can never be instantiated&lt;/em&gt;&lt;/strong&gt; (the very first point of distinction the search results provide us). Also some search result tell us that "abstract classes" should be defined by the &lt;em&gt;abstract&lt;/em&gt; keyword. well thank you !&lt;/p&gt;

&lt;p&gt;Secondly, a base class could also be a &lt;em&gt;concrete&lt;/em&gt; class. A concrete class is complete with all its implementations in place- just like a normal class and hence it can be instantiated !&lt;/p&gt;

&lt;p&gt;Coming to our abstract and virtual methods. &lt;/p&gt;

&lt;p&gt;By default any method be it of an abstract class or a concrete class cannot be overridden even if the class is inherited by some other class. So if we want a child class to have its own implementation of a particular base class method then we do so by marking the base class method as abstract or virtual -meaning allowing it to be overridden.&lt;/p&gt;

&lt;p&gt;Depending on the pizza which we order,say the price would vary , there might be certain discounts on a particular type of pizza, or some logic involved in calculating the price of the pizza. In the base class - Pizza we don't want to handle that , we just want our Pizza class to give us out a pizza base with a tomato sauce layered on it. In this case we will declare a method CalculatePrice as abstract&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;public abstract int CalculatePrice();&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 .&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Whenever we want the child classes to deal with implementation of a particular method we mark the method as an abstract.&lt;/p&gt;

&lt;p&gt;When doing so, we will only define the signature of the method in the parent class&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now the implications of this -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Like we established that the abstract classes are incomplete , abstract methods are also incomplete ! &lt;strong&gt;&lt;em&gt;Abstract methods do not have any default method implementation&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Given this any class inheriting from Pizza &lt;strong&gt;must&lt;/strong&gt; override this method ! Obviously it wouldn't make sense to have a method which is just a signature, without any job to do ! (Think about interfaces , how it is required for us to implement them) &lt;strong&gt;&lt;em&gt;It is mandatory to override abstract methods&lt;/em&gt;&lt;/strong&gt; !&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also since it is an incomplete method(without any implementation) it is pointless to have it in a concrete class (because concrete classes are complete and we cannot introduce this kind of ambiguity in them !). Hence the reason &lt;strong&gt;&lt;em&gt;Abstract methods can only be used in an abstract class&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3d6czxtwqehg6kpyizu6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3d6czxtwqehg6kpyizu6.jpg" alt="Alt Text" width="800" height="142"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What about virtual methods ?  No matter what, we want to have a basic pizza - a pizza dough base and tomato sauce on it (a l'il bit of cheese doesn't hurt as well!). Here I am sure that under any case I need to have that and not depend on the child classes for the implementation. I will go ahead and mark the method as virtual &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Whenever we want to have a default behavior or implementation of a method in a parent class we mark this method as virtual&lt;/p&gt;

&lt;p&gt;When we mark it as a virtual method ,in addition to giving a default implementation we also give liberty to the child classes to override it and have their own implementation defined if the default one doesn't meet their needs.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pizza&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;Price&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;abstract&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;CalculatePrice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Prepare&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Here is a pizza dough base with some tomato sauce spread on it !&lt;/span&gt;&lt;span class="dl"&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;Implications&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Though a default implementation is given in the parent class, the child classes can still have their implementations thus overriding the one given by the parent class.&lt;/li&gt;
&lt;li&gt;Since a virtual method is complete with a default implementation it is &lt;em&gt;not mandatory&lt;/em&gt; for the child classes to override it (have their own implementation). &lt;strong&gt;&lt;em&gt;Virtual methods don't necessarily have to be overridden&lt;/em&gt;&lt;/strong&gt; . If you don't override the &lt;em&gt;Prepare&lt;/em&gt; method in the &lt;em&gt;Pepperoni&lt;/em&gt; class you are not going to get Pepperoni,You get a basic Pizza !&lt;/li&gt;
&lt;li&gt;Also since they have a fully functional implementation , &lt;strong&gt;&lt;em&gt;they can be a part of both abstract and concrete class&lt;/em&gt;&lt;/strong&gt; !&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here we have inherited class &lt;em&gt;PepperoniPizza&lt;/em&gt; from the class &lt;em&gt;Pizza&lt;/em&gt; .You also see the inherited members of the Pizza class&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffxkzm3hp3l8n9nq7ixud.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffxkzm3hp3l8n9nq7ixud.jpg" alt="Alt Text" width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you find this useful.Any suggestions or fixes are always welcome ! :)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>csharp</category>
      <category>oop</category>
    </item>
    <item>
      <title>Break the LINQ - Dissecting the "Where" of LINQ</title>
      <dc:creator>Vidisha Parab</dc:creator>
      <pubDate>Fri, 27 Mar 2020 19:25:41 +0000</pubDate>
      <link>https://dev.to/vparab/break-the-linq-dissecting-the-where-of-linq-49l4</link>
      <guid>https://dev.to/vparab/break-the-linq-dissecting-the-where-of-linq-49l4</guid>
      <description>&lt;p&gt;I have been a .net developer for quite some time now and there are things which I have just blindly implemented by referring to code snippets without caring about their underlying intricacies, one of them being LINQ. I’m sure we all know what it stands for and why it is used so I won’t be going there.  What I really want to discuss here is the LINQ fluent functions which we use on a family of &lt;code&gt;IEnumerables&lt;/code&gt;. &lt;br&gt;
I always felt lazy to write these queries on my own and would always turn to online examples. I never bothered to look at the overloads and understand them. No more.Here is my very first attempt at writing a post.&lt;br&gt;
To write a LINQ query efficiently there are three things you must be aware of&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Delegates&lt;/li&gt;
&lt;li&gt; Lambda Expressions&lt;/li&gt;
&lt;li&gt; variety of extension methods offered on the &lt;em&gt;Enumerable&lt;/em&gt; class&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I would like to limit this post to dissect the most commonly used "Where" extension method. The documentation for it as below&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvl71euk9r5bx7ta8e23w.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvl71euk9r5bx7ta8e23w.JPG" alt="Alt Text" width="800" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first parameter preceded by &lt;em&gt;"this"&lt;/em&gt; keyword tells us right there itself that it is an extension method. Also notice that it returns an &lt;code&gt;IEnumerable&amp;lt;TSource&amp;gt;&lt;/code&gt;. So &lt;em&gt;Where&lt;/em&gt; is an extension method on a collection which is of type &lt;code&gt;IEnumerable&amp;lt;TSource&amp;gt;&lt;/code&gt; which also returns an &lt;code&gt;IEnumerable&amp;lt;TSource&amp;gt;&lt;/code&gt;. The collection referred here could be any collection which implements the IEnumerable interface e.g. List, IList.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo0din5w99x0xu9x7na1f.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo0din5w99x0xu9x7na1f.JPG" alt="Alt Text" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We move to the second parameter of the method &lt;code&gt;Func&amp;lt;TSource,bool&amp;gt; predicate&lt;/code&gt;. &lt;strong&gt;Func&lt;/strong&gt; is a built-in delegate in .NET. &lt;/p&gt;

&lt;p&gt;Delegate is a type to represent a method signature. E.g. if we have a method&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public string FormatNumber(int number)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;then we would write its “delegate” type as&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public delegate string FormatNumberDelegate(int number)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here the &lt;strong&gt;&lt;em&gt;FormatNumberDelegate&lt;/em&gt;&lt;/strong&gt; is a type that would represent the method &lt;strong&gt;&lt;em&gt;FormatNumber&lt;/em&gt;&lt;/strong&gt; or any method which accepts an integer and returns a string. If we were to represent this as &lt;em&gt;Func&lt;/em&gt; then the resultant delegate would be&lt;br&gt;
&lt;code&gt;Func&amp;lt;int,string&amp;gt;&lt;/code&gt; i.e. "Func" is any method which takes in an &lt;strong&gt;integer&lt;/strong&gt; parameter and returns a &lt;strong&gt;string&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Going back to our &lt;em&gt;Where&lt;/em&gt; extension, &lt;code&gt;Func&amp;lt;TSource,bool&amp;gt;&lt;/code&gt; represents any method which takes in a parameter of &lt;code&gt;TSource&lt;/code&gt; and returns a boolean. The boolean returned is essentially the condition which we would like to evaluate. If from a list of Employees, we want to filter the female employees, then we would write a condition which evaluates to true (a boolean) when the employee is a female. &lt;/p&gt;

&lt;p&gt;Lets say we have a class &lt;code&gt;Employee&lt;/code&gt; as follows&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;Employee&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;FirstName&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;string&lt;/span&gt; &lt;span class="n"&gt;LastName&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;string&lt;/span&gt; &lt;span class="n"&gt;Designation&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="n"&gt;DateTime&lt;/span&gt; &lt;span class="n"&gt;StartDate&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="n"&gt;DateTime&lt;/span&gt; &lt;span class="n"&gt;EndDate&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="n"&gt;Gender&lt;/span&gt; &lt;span class="n"&gt;Gender&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Gender&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;M&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so our condition would be &lt;br&gt;
&lt;code&gt;if(employee.Gender == Gender.F) return true;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The simplest way to write this is&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;bool&lt;/span&gt; &lt;span class="nf"&gt;FilterFemaleEmployees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gender&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Gender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;false&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;and then we can use it with our &lt;em&gt;Where&lt;/em&gt; extension as &lt;code&gt;var femaleEmployees = employees.Where(FilterFemaleEmployees);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Comparing this with the documentation for the &lt;em&gt;Where&lt;/em&gt; extension,&lt;br&gt;
&lt;em&gt;TSource&lt;/em&gt; = Employee&lt;br&gt;
&lt;code&gt;IEnumerable&amp;lt;TSource&amp;gt;&lt;/code&gt; = &lt;code&gt;var femaleEmployees is IEnumerable&amp;lt;Employee&amp;gt;&lt;/code&gt; which is returned by the &lt;em&gt;Where&lt;/em&gt; method&lt;br&gt;
&lt;code&gt;Func&amp;lt;TSource,bool&amp;gt;&lt;/code&gt; = &lt;code&gt;bool FilterFemaleEmployees(Employee employee)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets take it to one level up by making use of anonymous delegate. &lt;/p&gt;

&lt;p&gt;The FilterFemaleEmployees is a &lt;strong&gt;&lt;em&gt;named&lt;/em&gt;&lt;/strong&gt; delegate which is good but while using LINQ the main focus is on compact queries , writing efficient code with as few lines as possible.&lt;br&gt;
With an anonymous delegate, the query would look like&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;femaleEmployees&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;delegate&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gender&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Gender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;false&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;whenever we are evaluating a condition, its result is going to return a boolean. i.e if we just return employee.Gender == Gender.F , it is going to return a true or a false based on the value of employee.Gender. So reducing the code further,&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;femaleEmployees&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;delegate&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt; &lt;span class="n"&gt;employee&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;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gender&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Gender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;F&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;Now comes the lambda ! Lambdas are a great way to represent an anonymous function. There are three parts to a lambda expression :&lt;br&gt;
the parameters, the =&amp;gt; operator and the method body (statement or expressions).&lt;br&gt;
We remove the delegate keyword and replace it with the following&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;femaleEmployees&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;)=&amp;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;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gender&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Gender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;F&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;Another good feature of a lambda is the parameter inference. In the above, we are supplying a parameter of type Employee to the anonymous function. But here we can make good use of the parameter inference and let the compiler figure out its type (another step towards compact code)&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;femaleEmployees&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;)=&amp;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;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gender&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Gender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;F&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;If you hover over employee in visual studio, you would see the compiler has rightly interpreted its type&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flixnapcszeock3v564c7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flixnapcszeock3v564c7.jpg" alt="Alt Text" width="800" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another cool feature of lambda expressions is if we have a single parameter we don't need to enclose it in brackets, we can get rid of those&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;femaleEmployees&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;=&amp;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;employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gender&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Gender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;F&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;More to compact coding !!&lt;/p&gt;

&lt;p&gt;If we are having just a single statement and returning a single value we can well get rid of the curlys and the return statement too !&lt;br&gt;
 &lt;code&gt;var femaleEmployees = employees.Where( employee=&amp;gt; employee.Gender == Gender.F);&lt;/code&gt;&lt;br&gt;
And since we are inclined to have as much less written code as possible, a convention followed while writing lambda expressions is to name the parameters with single letters further reducing our code to&lt;br&gt;
&lt;code&gt;var femaleEmployees = employees.Where( e=&amp;gt; e.Gender == Gender.F);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's it ! That's how we reach the where of "Where" came from ! If you understand the Func delegate and the lambdas ,it is going to be easy to decipher and use those LINQ extension methods. You could evaluate even more complex conditions through the &lt;em&gt;Where&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;A similar understanding can be extended to write the following extension methods as well &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Any&lt;/em&gt;&lt;br&gt;
&lt;code&gt;public static bool Any&amp;lt;TSource&amp;gt; (this System.Collections.Generic.IEnumerable&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource,bool&amp;gt; predicate);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FirstOrDefault&lt;/em&gt;&lt;br&gt;
&lt;code&gt;public static TSource FirstOrDefault&amp;lt;TSource&amp;gt; (this System.Collections.Generic.IEnumerable&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource,bool&amp;gt; predicate);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;SingleOrDefault&lt;/em&gt;&lt;br&gt;
&lt;code&gt;public static TSource SingleOrDefault&amp;lt;TSource&amp;gt; (this System.Collections.Generic.IEnumerable&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource,bool&amp;gt; predicate);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I would have loved to discuss about &lt;em&gt;Join&lt;/em&gt; and the &lt;em&gt;GroupBy&lt;/em&gt; but I will save it to a later post.&lt;/p&gt;

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