<?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: Isaac Ayodeji Ikusika</title>
    <description>The latest articles on DEV Community by Isaac Ayodeji Ikusika (@ayodejii).</description>
    <link>https://dev.to/ayodejii</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%2F513577%2F8be3800e-2016-498d-8ae3-74996ae63e9c.png</url>
      <title>DEV Community: Isaac Ayodeji Ikusika</title>
      <link>https://dev.to/ayodejii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayodejii"/>
    <language>en</language>
    <item>
      <title>Using Git on Visual Studio</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Mon, 23 Jan 2023 18:05:26 +0000</pubDate>
      <link>https://dev.to/ayodejii/using-git-on-visual-studio-2gfn</link>
      <guid>https://dev.to/ayodejii/using-git-on-visual-studio-2gfn</guid>
      <description>&lt;p&gt;Visual Studio is a common tool with dotnet developers. It is an IDE where you can do all your developments without needing any other tool, even database management software. Last week, I saw someone who has Visual Studio use git bash to manage their git repo. I showed them how to use the git tool on the IDE and thought to write something about it.&lt;/p&gt;

&lt;p&gt;You can clone an existing repo or start a new project entirely from Visual Studio without having to use any terminal. Everything you need is already in the IDE. &lt;/p&gt;

&lt;p&gt;Let us start with cloning an existing repo.&lt;/p&gt;

&lt;p&gt;When you open your Visual Studio (version &amp;gt;= 2019), you get this screen. &lt;/p&gt;

&lt;p&gt;Let's assume you have a repo you want to clone.&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%2Fo3d1l22oktocrctohq3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3d1l22oktocrctohq3m.png" alt="Visual studio screen highlighting cloning an existing repo" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on "Clone a repository" and you will get this page&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%2Fq3fbvz078rcili4jnhqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3fbvz078rcili4jnhqh.png" alt="clone repository page" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Put the link of the repo you want to clone and click on clone&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%2Fk4lfe88kal48nai0mdtp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk4lfe88kal48nai0mdtp.png" alt="clone repo page pointing to the clone button" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila, you have successfully cloned a repo.&lt;/p&gt;

&lt;p&gt;To create a new repo from a new project, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create your project&lt;/li&gt;
&lt;/ol&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%2Fm5ggtc4geowm9unm3n8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5ggtc4geowm9unm3n8w.png" alt="create new project" width="800" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;After your project is created, click on the "Git changes" tab beside the Solution Explorer.&lt;/li&gt;
&lt;/ol&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%2Fmi9exvg5g0i0g93sa7e8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmi9exvg5g0i0g93sa7e8.png" alt="git changes tab" width="743" height="667"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you cannot find the "Git changes" tab, click the View tab at the top of Visual Studio, you will find "Git changes" there.&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%2F12hix4cqy6v3skct8dpw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F12hix4cqy6v3skct8dpw.png" alt="git changes location on top of visual studio" width="756" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the Git changes tab come up, click on "Create new repository" and you will get this page&lt;/li&gt;
&lt;/ol&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%2F9d7mnwtv8sfb72sbe5b3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9d7mnwtv8sfb72sbe5b3.png" alt="create new repo page" width="800" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fill the form with everything you want and you are good to go. Subsequent changes can be made in the "Git changes" tab.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;NB: You must have your git set up before you can do all these. You may get some prompt to set up your git if you have not.&lt;/p&gt;

</description>
      <category>community</category>
      <category>discuss</category>
    </item>
    <item>
      <title>C# Open-Source Projects for beginners</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Fri, 09 Dec 2022 15:26:05 +0000</pubDate>
      <link>https://dev.to/ayodejii/c-open-source-projects-for-beginners-2o29</link>
      <guid>https://dev.to/ayodejii/c-open-source-projects-for-beginners-2o29</guid>
      <description>&lt;p&gt;Projects to contribute to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/ayodejii/blazor-spinner-css"&gt;Blazor-Spinner-CSS&lt;/a&gt; - 90% CSS and 10% C#&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/WJSoftware/wj.Ocelot.Configuration"&gt;wj.Ocelot.Configuration&lt;/a&gt; - 100% C#&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many developers and beginners want to contribute to opensource projects, but they find it hard to due to these reasons and more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Advanced projects: Most of the available projects are too advanced for beginners. A glance at the first code file already discourages them, not to mention the senior contributors of that project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inadequate open-source projects: This can also derive from the first point. I do check trending opensource C# projects every day and what I see are mostly official Microsoft projects. There are fewer individual projects available, well, maybe not trending.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These two points said, I have taken it upon myself to look for opensource C# projects and update this post with them.&lt;br&gt;
If you find some, please add in the comments too.&lt;br&gt;
Thank you 😊👨🏿‍💻👩🏿‍💻&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>beginners</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Type safety and strong typing—what are these terms?</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Sun, 09 Oct 2022 09:41:08 +0000</pubDate>
      <link>https://dev.to/ayodejii/type-safety-and-strong-typing-what-are-these-terms-3io5</link>
      <guid>https://dev.to/ayodejii/type-safety-and-strong-typing-what-are-these-terms-3io5</guid>
      <description>&lt;p&gt;"C# is a strongly typed and type safe language". This is one of the first sentences you see as a C# newbie. I did not really care about the meaning of these words when I started out and I think it mostly is because I only saw them as a features of C# and nothing to worry about.&lt;/p&gt;

&lt;p&gt;I understood that you cannot implicitly assign some types to each other and that was just it. I also believed that type safety and strong typing have the same meaning.&lt;/p&gt;

&lt;p&gt;But I was wrong.&lt;/p&gt;

&lt;p&gt;The type safety feature of a programming language simply means the language only allows you to perform the operations allowed by a data type.&lt;br&gt;
Take for example, you cannot perform a &lt;code&gt;IsNullOrEmpty()&lt;/code&gt; check on an integer value in C# because it is only allowed on string types.&lt;/p&gt;

&lt;p&gt;This permission can happen during runtime and/or compile time, and that is when strong (or static) typing comes in. Strong typing means the permission check will happen at compile time while weak typing means the check will happen at runtime by throwing an exception.&lt;/p&gt;

&lt;p&gt;In our example above, the compiler will tell us &lt;code&gt;cannot convert from int to string&lt;/code&gt;, making C# strongly typed and type safe.&lt;/p&gt;

&lt;p&gt;JavaScript conversely is weakly (or dynamically) typed but type safe. An example of this is how it provides you with a well-defined way to convert a string to an integer, but only gives you an error at runtime when you are parsing the wrong string.&lt;/p&gt;

&lt;p&gt;It is advised to use generic collections in C# because the non-generic ones are not type safe. Check the examples below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System.Collections

//Stack
Stack myStack = new();
myStack.Push("Hello");
myStack.Push(23);

//Queues
Queue myQ = new();
myQ.Enqueue("Hello");
myQ.Enqueue(23);

//ArrayList
ArrayList myAL = new();
myAL.Add("Hello");
myAL.Add(23);
myAL.Add(DateTime.Now);

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

&lt;/div&gt;



&lt;p&gt;Notice that these types allow addition of any data type in them, unlike generic ones, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using System.Collections.Generic

❌
Stack&amp;lt;int&amp;gt; myStack = new();
myStack.Push("Hello"); // =&amp;gt; compile time error because the only int type is allowed
myStack.Push(23);

✅
Stack&amp;lt;int&amp;gt; myStack = new();
myStack.Push(80);
myStack.Push(23);

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

&lt;/div&gt;



</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
    </item>
    <item>
      <title>User-defined Conversions: Conversion between Custom Types</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Sat, 20 Aug 2022 06:18:00 +0000</pubDate>
      <link>https://dev.to/ayodejii/user-defined-conversions-conversion-between-custom-types-6l8</link>
      <guid>https://dev.to/ayodejii/user-defined-conversions-conversion-between-custom-types-6l8</guid>
      <description>&lt;p&gt;Have you ever got this compile time error &lt;code&gt;error CS0029: Cannot implicitly convert type 'string' to 'int'&lt;/code&gt;? This happens when you want to pass a &lt;code&gt;string&lt;/code&gt; value into an &lt;code&gt;int&lt;/code&gt; variable.&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;count&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;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"20"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//you get the error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because C# is statically typed, it will not allow it, hence, the error.&lt;/p&gt;

&lt;p&gt;JavaScript is dynamically typed at compile time, so it allows conversion of any type to another:&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// no error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another instance where you get the error in C#:&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;long&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myIntCount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can only solve this problem by converting from one type to another.&lt;/p&gt;

&lt;p&gt;Let's talk about the main kinds of type conversion we have in C# dotnet.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implicit conversion: This is straightforward. No exception throw, no data lost. The conversion always succeeds. Example of this is when you are converting a small numerical type to a larger one:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;numberOfCount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Explicit conversion: This is required when information might be lost in the conversion or when the conversion does not &lt;em&gt;just&lt;/em&gt; succeed. Here, we use cast expression. An example is converting a larger numerical type to a small one:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;numberOfCount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;User-defined conversion: This is our main topic. It is performed by writing a custom method that does the conversion implicitly or explicitly. It is done to convert a custom type to another primitive or custom type.
There are some rules to follow to get your conversion working.

&lt;ul&gt;
&lt;li&gt;the conversion methods must be public and static&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;implicit&lt;/code&gt; and &lt;code&gt;explicit&lt;/code&gt; keywords to define the kind of conversion you are implementing. As explained above, implicit conversions do not throw exception or data loss unlike explicit conversions.&lt;/li&gt;
&lt;li&gt;The type (or class) where you have the conversion method must be either a source type or a target type of that conversion. This means if you are converting from type A to type B, the conversion can be defined in either of the two types. For conversion from a custom to primitive type, the conversion is defined in the custom type.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at an example.&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="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&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;public&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//we added a constructor so we could initialise this type with a value &lt;/span&gt;
&lt;span class="c1"&gt;//and then add a check and throw an exception. &lt;/span&gt;
&lt;span class="c1"&gt;//It is not compulsory you do this.&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;age&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You are too young for this conversion."&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="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&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;static&lt;/span&gt; &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="kt"&gt;int&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;person&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;person&lt;/span&gt;&lt;span class="p"&gt;.&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;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&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;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;string&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;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;$"I am &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the code above, we have a &lt;code&gt;Person&lt;/code&gt; type that we want to convert to an integer and back.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;age&lt;/code&gt; field in &lt;code&gt;Person&lt;/code&gt; is an integer and can be easily converted to &lt;code&gt;int&lt;/code&gt; without throwing an exception, we use an implicit conversion, hence, &lt;br&gt;
&lt;code&gt;public static implicit operator int(Person person) =&amp;gt; person.age;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is not true for a conversion of &lt;code&gt;int&lt;/code&gt; to &lt;code&gt;Person&lt;/code&gt; because it naturally cannot occur implicitly and it could throw an exception if the &lt;code&gt;age&lt;/code&gt; is less than &lt;code&gt;16&lt;/code&gt;. So, we do an explicit conversion:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public static explicit operator Person(int age) =&amp;gt; new Person(age);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's test our conversion:&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;person&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;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;17&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&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="c1"&gt;//converting Person type to integer&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="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// output: 17&lt;/span&gt;

&lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;anotherPerson&lt;/span&gt; &lt;span class="p"&gt;=&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;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//converting integer type to Person&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="n"&gt;anotherPerson&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// output: 17&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;That was conversion of a custom type to primitive type and back.&lt;/p&gt;

&lt;p&gt;Now let's look at conversion of a custom type to another.&lt;/p&gt;

&lt;p&gt;Let's add another type:&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;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="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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="nf"&gt;Person&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;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Person&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;Age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//conversion of Employee to Person&lt;/span&gt;

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

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

&lt;/div&gt;



&lt;p&gt;There is nothing particularly different from what we explained before in this code. We are converting from &lt;code&gt;Employee&lt;/code&gt; to &lt;code&gt;Person&lt;/code&gt;, so we have a conversion method that does that, using the &lt;code&gt;explicit&lt;/code&gt; keyword because of an exception that could happen.&lt;/p&gt;

&lt;p&gt;Please note that we can have this method on the &lt;code&gt;Person&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Testing this, we have:&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;employee&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;Employee&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Age&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;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"John"&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;myEmployeePerson&lt;/span&gt; &lt;span class="p"&gt;=&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;employee&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="n"&gt;myEmployeePerson&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="c1"&gt;//output: I am 23 years old&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;What I enjoy about C# is its extensibility. You should make use of this every chance you get. Type conversion is one of them and it will help you write cleaner code.&lt;br&gt;
You can read more about this on &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators"&gt;Microsoft docs&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding the Enumerable.Aggregate method in LINQ</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Sun, 07 Aug 2022 08:44:00 +0000</pubDate>
      <link>https://dev.to/ayodejii/understanding-the-enumerableaggregate-method-in-linq-5dpl</link>
      <guid>https://dev.to/ayodejii/understanding-the-enumerableaggregate-method-in-linq-5dpl</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL/DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;Aggregate&lt;/code&gt; method is an extension method of collections that inherit from &lt;code&gt;IEnumerable&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the method returns only one value of any type&lt;/li&gt;
&lt;li&gt;It is an accumulation function same as sum, count etc.&lt;/li&gt;
&lt;li&gt;it has 3 overloads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some weeks ago, I had to display the expected total amount of money to be debited from a list of account in a specific year, month and week, starting from the first day the account was added to the system, based on a condition.&lt;/p&gt;

&lt;p&gt;This is different from the summation of all the money to be debited from the account, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;there is a condition to be followed in each iteration of the list of accounts&lt;/li&gt;
&lt;li&gt;my return value is a tuple i.e. a tuple of three int values representing week, month and year deductions &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The preferred method to use in this case is the &lt;code&gt;Aggregate&lt;/code&gt; method. It is present in all C# collections that inherits from &lt;code&gt;IEnumerable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Aggregate&lt;/code&gt; method performs an operation on each element in a list, taking the previous operations into account. For example, it performs an operation on the first two elements of a list and uses the result to operate on the third element and on and on to return a single value.&lt;/p&gt;

&lt;p&gt;Consider 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;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&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="m"&gt;2&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="m"&gt;4&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="c1"&gt;//we use aggregate to get the summation of the elements of the list&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;summation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Aggregate&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//summation will be 1 + 2 + 3 + 4 + 5 = 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Accumulation Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Aggregate&lt;/code&gt; method is an accumulation function. An accumulation function combines the rows in a collection, performs an operation on each one of them and returns a single value. There are different accumulation functions in LINQ: Sum, Max, Min, Count and Average, but the &lt;code&gt;Aggregate&lt;/code&gt; method can perform all the operations these methods perform and more.&lt;/p&gt;

&lt;p&gt;One other advantage of &lt;code&gt;Aggregate&lt;/code&gt; is that it performs operations on a collection of any type, unlike others that only work with numerical data types.&lt;/p&gt;

&lt;p&gt;Consider 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="c1"&gt;//flatten a list of countries to a string separated by comma&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;countries&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Nigeria"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Ghana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Togo"&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;countriesToString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;countries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Aggregate&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&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;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//countriesToString will be "Nigeria, Ghana, Togo"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Overloads of the Aggregate Method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Aggregate&lt;/code&gt; method has three overloads, the first one is explained and used above.&lt;br&gt;
However, I will add the signature of the first overload here and explain in C# terms.&lt;br&gt;
The three overloads are:&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="m"&gt;1.&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TSource&lt;/span&gt; &lt;span class="n"&gt;Aggregate&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="n"&gt;Func&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="m"&gt;2.&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TAccumulate&lt;/span&gt; &lt;span class="n"&gt;Aggregate&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TAccumulate&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="n"&gt;TAccumulate&lt;/span&gt; &lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="n"&gt;Func&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TAccumulate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TAccumulate&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="m"&gt;3.&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TResult&lt;/span&gt; &lt;span class="n"&gt;Aggregate&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TAccumulate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="n"&gt;TAccumulate&lt;/span&gt; &lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="n"&gt;Func&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TAccumulate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TAccumulate&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="n"&gt;Func&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TAccumulate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;resultSelector&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The aggregate method is generic, meaning it takes and returns any type, hence, the &lt;code&gt;Aggregate&amp;lt;TSource&amp;gt;&lt;/code&gt; and others.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TSource&lt;/code&gt; here is the type of the elements of the &lt;code&gt;Enumerable&lt;/code&gt; we are aggregating over. The &lt;code&gt;this&lt;/code&gt; keyword here shows that the &lt;code&gt;Aggregate&lt;/code&gt; function is an &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods"&gt;extension method&lt;/a&gt; of &lt;code&gt;IEnumerable&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;source&lt;/code&gt; is the collection we are aggregating over.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Func&amp;lt;TSource, TSource, TSource&amp;gt;&lt;/code&gt; is a &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.func-2?view=net-6.0"&gt;delegate&lt;/a&gt; that returns the last &lt;code&gt;TSource&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the same for other overloads. It represents the accumulator operation to be invoked on each element of the collection.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The second and third overloads have a parameter called seed i.e. &lt;code&gt;TAccumulate seed&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This represents the initial accumulator value. It could be any type, it is saying, "start with this value".&lt;/p&gt;

&lt;p&gt;The value of this seed determines the return value of the &lt;code&gt;Aggregate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let us use our numbers example from above.&lt;br&gt;
Let's say we want to add 10 to the sum before adding other elements, this is what we do:&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;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&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="m"&gt;2&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="m"&gt;4&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="c1"&gt;//we use the seed parameter:&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;summation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Aggregate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//summation will be 10 + 1 + 2 + 3 + 4 + 5 = 25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last overload has an extra delegate &lt;code&gt;Func&amp;lt;TAccumulate, TResult&amp;gt; resultSelector&lt;/code&gt;, which is a function to transform the final value into the result value we want. &lt;/p&gt;

&lt;p&gt;It means since the &lt;code&gt;Aggregate&lt;/code&gt; value returns only one value, we can decide to perform another operation on the result we got.&lt;/p&gt;

&lt;p&gt;Let's practice this with our countries example from above.&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;//flatten a list of countries to a string separated by comma&lt;/span&gt;
&lt;span class="c1"&gt;//add another west African country to the start of the list&lt;/span&gt;
&lt;span class="c1"&gt;//then convert these countries to upper case&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;countries&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Nigeria"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Ghana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Togo"&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;countriesToUpper&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;countries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Aggregate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Liberia"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&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;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;countryString&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;countryString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToUpper&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;//countriesToUpper will be "LIBERIA, NIGERIA, GHANA, TOGO"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NB: There is a better way to write the code above. We can convert the countries to upper case without the use of this overload.&lt;/p&gt;

&lt;p&gt;Instead of using a lambda, we can use define a method outside the code block and use it inside the &lt;code&gt;Aggregator&lt;/code&gt; method.&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;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&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="m"&gt;2&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="m"&gt;4&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="c1"&gt;//we use the seed parameter:&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Aggregate&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;doMathsOnNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;doMathsOnNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;second&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;first&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&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;first&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;second&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;first&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;//result will be 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is fun and you should try it out. It sure will come handy one of these days.&lt;br&gt;
You can read more on the &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=net-6.0"&gt;Microsoft docs&lt;/a&gt; for this method.&lt;/p&gt;

&lt;p&gt;Happy coding. 🚀&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>csharp</category>
    </item>
    <item>
      <title>More on Number Systems: Working with Base Numbers: Part 2</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Sun, 31 Jul 2022 08:39:42 +0000</pubDate>
      <link>https://dev.to/ayodejii/more-on-number-systems-working-with-base-numbers-part-2-mpa</link>
      <guid>https://dev.to/ayodejii/more-on-number-systems-working-with-base-numbers-part-2-mpa</guid>
      <description>&lt;p&gt;The &lt;a href="https://dev.to/ayodejii/more-on-number-systems-working-with-base-numbers-part-1-4lk0"&gt;first part of this topic&lt;/a&gt; introduced and took us through number bases and converting them to base 10.&lt;br&gt;
This post will explain what base 16 numbers are and how to convert from base 10 to other numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hexadecimals&lt;/strong&gt;&lt;br&gt;
This is base16. It is represented with numbers and alphabets A to F.&lt;br&gt;
Beginners think it is a complex base because of the alphanumeric representation, but it is not, it is just like the other ones. The alphabet is just a way to make the numbers cleaner. They represent numbers 10-15 because 16 is the maximum number in this base.&lt;br&gt;
For example:&lt;br&gt;
9 base 10 is 9 in base 16&lt;br&gt;
4 base 10 is 4 in base 16&lt;br&gt;
10 base 10 is A in base 16&lt;br&gt;
11 is B&lt;br&gt;
15 is F&lt;br&gt;
16 is 10&lt;br&gt;
493 is 1ED&lt;br&gt;
The reason for the inclusion of the alphabets is, HEX numbers get to tens of numbers in base 10. For example, the conversion of 493 gives &lt;code&gt;1 14 13&lt;/code&gt; which is confusing because everyone would read 11413 at a glance, hence a way to make it clearer for readers: 1 E(14) D(13) i.e. 1ED 😀.&lt;/p&gt;

&lt;p&gt;Colour white in CSS, #fff is a hexadecimal number, which, when converted, gives 4095. Wonder why colour codes are represented this way?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conversion of decimal numbers to other bases&lt;/strong&gt;&lt;br&gt;
We learned last week that base 10 numbers are decimals. So let us see how to convert these numbers to other bases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4 to base 2&lt;/li&gt;
&lt;li&gt;11 to base 8&lt;/li&gt;
&lt;li&gt;493 to base 16&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the last conversion from other bases to decimal, we used multiplication. Here, we are going to use division.&lt;br&gt;
To convert a decimal number to another base, you keep dividing it by that base until you have 0 left and the remainder is less than that base then you read the remainder from bottom to top.&lt;/p&gt;

&lt;p&gt;4 =&amp;gt; 4/2 = 2 r 0&lt;br&gt;
     2/2 = 1 r 0&lt;br&gt;
     1/2 = 0 r 1&lt;br&gt;
our answer is 100.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11 to base 8&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;11 =&amp;gt; 11/8 = 1 r 3&lt;br&gt;
      1/8 = 0 r 1&lt;br&gt;
our answer is 13&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;493 to base 16&lt;/strong&gt;&lt;br&gt;
493 =&amp;gt; 493/16 = 30 r 13&lt;br&gt;
       30/16 = 1 r 14&lt;br&gt;
       1/16 = 0 r 1&lt;br&gt;
we have 1 14 13, which is 1ED in hexadecimal&lt;/p&gt;

&lt;p&gt;To convert from a base other than decimal to another, you need to convert to decimal before you do the final conversion.&lt;/p&gt;

&lt;p&gt;You can do these conversions on your calculator, but I like to think it is a good practice to understand how things work under the hood.&lt;/p&gt;

&lt;p&gt;Enjoy. 🚀&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>More on Number Systems: Working with Base Numbers: Part 1</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Sat, 23 Jul 2022 05:28:00 +0000</pubDate>
      <link>https://dev.to/ayodejii/more-on-number-systems-working-with-base-numbers-part-1-4lk0</link>
      <guid>https://dev.to/ayodejii/more-on-number-systems-working-with-base-numbers-part-1-4lk0</guid>
      <description>&lt;p&gt;In my &lt;a href="https://dev.to/ayodejii/enum-flags-in-c-20a6"&gt;previous post&lt;/a&gt;, I talked about working with binary and bitwise operators. I realized that for us to really understand how these operators work, we need to understand two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;base numbers&lt;/li&gt;
&lt;li&gt;how the operators work with these numbers under the hood&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post talks about base numbers, and how we come by them.&lt;br&gt;
It is divided into bits (permit my pun), so the post will not be too long and boring.&lt;/p&gt;

&lt;p&gt;Base numbers are numbers that are used to represent numeric values on the number system. They are basically numbers. Nothing more. &lt;br&gt;
They just occur in other bases, the most popular being base10 or &lt;strong&gt;Decimal&lt;/strong&gt;.&lt;br&gt;
base 2 is &lt;strong&gt;Binary&lt;/strong&gt;&lt;br&gt;
base 8 is &lt;strong&gt;Octal&lt;/strong&gt;&lt;br&gt;
base 16 is &lt;strong&gt;HexaDecimal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The way to represent these bases is by saying n subscript base, e.g., 2 subscript 8 is 2 base 8. I cannot find a way to represent this in this post.&lt;br&gt;
Base 10 numbers do not use these subscripts because we assume all numbers are in base 10 unless specified.&lt;br&gt;
Numbers can occur in many bases, these are just the most common ones.&lt;br&gt;
Base10 numbers are the traditional numbers we are used to: 1, 2, 3...9, 10.&lt;/p&gt;

&lt;p&gt;Base numbers follow a pattern. Each base has a maximum representation of that base minus 1. For base 10, it is 9, 2 is 1 and 8 is 7. We are going to exemplify this with base10.&lt;br&gt;
1, 2, ... 8, 9, 10, 11, 12, 13...98, 99, 100, 101 etc.&lt;/p&gt;

&lt;p&gt;Notice that when the "numbers" get to 9, they went back to 1 and 0, same as when it got to 99, we added two zeros to the right side of 1. That is how tens, hundreds, thousands, etc. are represented - by the rightmost number.&lt;br&gt;
This same pattern goes for other bases:&lt;br&gt;
2 in base two is 10&lt;br&gt;
8 in base eight is 10&lt;br&gt;
16 in base 16 is 10 etc.&lt;/p&gt;

&lt;p&gt;How this works is that the numbers start off as unit, then when you get to the maximum representation, you go to tens, and keep adding 1 till you get to the maximum number again.&lt;br&gt;
That is why:&lt;br&gt;
11 is 11 in base 10&lt;br&gt;
3 is 11 in base 2&lt;br&gt;
9 is 11 in base 8&lt;br&gt;
17 is 11 in base 16.&lt;/p&gt;

&lt;p&gt;We are going to learn more about base 2 and base 16 in the next lesson.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conversion across Bases&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, we are going to talk about converting from other bases to base 10, then from 10 to other bases, and then across bases that are not 10.&lt;/p&gt;

&lt;p&gt;Conversion from a base to base 10 is straightforward. You just raise each unit of the number to the power of its bases reducing it by index of each unit of the number.&lt;/p&gt;

&lt;p&gt;For example, let's convert these numbers to base 10:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;11 base 8&lt;/li&gt;
&lt;li&gt;207 base 8&lt;/li&gt;
&lt;li&gt;11 base 2&lt;/li&gt;
&lt;li&gt;81 base 4&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11 base 8 = 1*8^1 + 1*8^0 = 8 + 1 = 9 base 10&lt;br&gt;
207 base 8 = 2*8^2 + 0*8^1 + 7*8^0 = 64 + 0 + 7 = 71&lt;br&gt;
11 base 2 = 1*2^1 + 1*2^0 = 2 + 1 = 3&lt;br&gt;
81 base 4 = 8*4^1 + 8*4^0 = 32 + 8 = 40 &lt;/p&gt;

&lt;p&gt;Please note that ^ represents power e.g. 2^2 is 2*2 = 4, 3^3 is 3*3*3 = 24&lt;/p&gt;

&lt;p&gt;This post is, as a matter of fact, more about mathematics than coding, but trust me, you need it to advance in your career as a software developer.&lt;br&gt;
Please stay tuned for the continuation. 😀&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Enum Flags in C#</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Sat, 09 Jul 2022 07:36:29 +0000</pubDate>
      <link>https://dev.to/ayodejii/enum-flags-in-c-20a6</link>
      <guid>https://dev.to/ayodejii/enum-flags-in-c-20a6</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;code&gt;Flags&lt;/code&gt; attribute on your enum type&lt;/li&gt;
&lt;li&gt;Use bit values as the enum representation because it allows you to use bitwise operators&lt;/li&gt;
&lt;li&gt;Add up enums into a variable using the "|" operator&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;HasFlags()&lt;/code&gt; method to determine whether one or more bit fields are set in an enumeration value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You must have worked on a feature that involves switching between multiple options, and if you are a C# developer, you know you could easily do that with the &lt;code&gt;enum&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;enum&lt;/code&gt; type is a built-in value type which is the short form of the Enumeration type. &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum"&gt;Microsoft&lt;/a&gt; describes the &lt;code&gt;enum&lt;/code&gt; as a type defined by a set of named constants of the underlying integral numeric type. This means that instead of having your options as numerical values, enums provide a way for you to use their representation.&lt;/p&gt;

&lt;p&gt;A typical enum type is 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="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;ColorTypicalEnum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;None&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;Black&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;Red&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;Green&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;Blue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&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;int&lt;/code&gt; type in front of the type tells the compiler the numerical type you want your &lt;code&gt;enum&lt;/code&gt; represented as.&lt;/p&gt;

&lt;p&gt;Flags in &lt;code&gt;enum&lt;/code&gt; is an attribute that indicates that an enum type can be treated as a bit field. These bit fields are now grouped in a set of flags.&lt;br&gt;
Here, you can represent the int in form of the enum fields as a bit. This makes working with the enum easier than the typical type.&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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Flag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;ColorWithFlag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;None&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;Black&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;Red&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;Green&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;Blue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="c1"&gt;//after some really, really long color listing&lt;/span&gt;
   &lt;span class="n"&gt;ThisColorIsSoRare&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;268435456&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bit values here would be hard to comprehend and even easier to get wrong, e.g., how is &lt;code&gt;ThisColorIsSoRare = 268435456&lt;/code&gt;? Good question, that is why you use &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators"&gt;bitwise operations&lt;/a&gt; instead.&lt;br&gt;
So, we use the &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#left-shift-operator-"&gt;left shift operator&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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Flag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;ColorWithFlag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;None&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;Black&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;Red&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//2&lt;/span&gt;
   &lt;span class="n"&gt;Green&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//4&lt;/span&gt;
   &lt;span class="n"&gt;Blue&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//8&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;
   &lt;span class="n"&gt;ThisColorIsSoRare&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//268435456&lt;/span&gt;
   &lt;span class="n"&gt;AllColors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to the most interesting part of using flags. Because the &lt;code&gt;Flag&lt;/code&gt; attribute allows you to use bitwise operations, you can easily do cool stuff 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="c1"&gt;// group all the primary colors (i.e. red, blue, and green) into one variable&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;primaryColors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ColorWithFlag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Red&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;ColorWithFlag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Green&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; 
                    &lt;span class="n"&gt;ColorWithFlag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Blue&lt;/span&gt;

&lt;span class="c1"&gt;//this sums the integer representation of the color in the `primaryColors` variable, so if you do&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;primaryColorsIntegerValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;primaryColors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//you would get 14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check if a color exists in the primary color variable using the &lt;code&gt;HasFlag&lt;/code&gt; (which inner workings is a bitwise operation) method.&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;isPrimaryColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;primaryColors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;HasFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ColorWithFlag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Black&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//this returns false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a massive chance you use Enums in your present or next project, so you be ready to use this knowledge of flags. It will make your code cleaner.&lt;/p&gt;

&lt;p&gt;Please do check the provided links for a better understanding and examples.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>beginners</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Test Driven Development - Explained</title>
      <dc:creator>Isaac Ayodeji Ikusika</dc:creator>
      <pubDate>Thu, 04 Nov 2021 04:45:29 +0000</pubDate>
      <link>https://dev.to/ayodejii/test-driven-development-explained-1fgh</link>
      <guid>https://dev.to/ayodejii/test-driven-development-explained-1fgh</guid>
      <description>&lt;p&gt;Test Driven Development (TDD) in layman's language is a way of testing a piece of code before writing the actual code.&lt;/p&gt;

&lt;p&gt;The first time I saw this explained, I was confused. It was completely abstract to me - How or why would I test a code I have not written? I think I felt this way because most of the research I did concerning this topic was always about adding two numbers or did not use [near] real examples, so I'm going to use my story to explain this term in this article.&lt;/p&gt;

&lt;p&gt;I used to work in a company that writes API for financial companies, so I was given a task - display all active customers that have more than $400 in their account.&lt;br&gt;
Easy task! I wrote the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var activeCustomersWith400 = Customers.Where(cus =&amp;gt; cus.IsActive &amp;amp;&amp;amp; cus.Balance &amp;gt; 400)

//pardon my C# and LINQ 😏
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, that's correct, then I wrote the test for it after manually testing/making sure my code works - by debugging. That is not TDD approach.&lt;/p&gt;

&lt;p&gt;TDD approach is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating test methods (or a class with methods) specifically for that task to:

&lt;ul&gt;
&lt;li&gt;test that the customers returned are active&lt;/li&gt;
&lt;li&gt;test that they have $400 in their account&lt;/li&gt;
&lt;li&gt;any other test you want, making sure they go along with the requirements of the task.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Running the tests. They will obviously fail, or your IDE or compiler complains about errors, since you do not have the function that does the task in your actual code yet.&lt;/li&gt;
&lt;li&gt;Fix the errors by writing the function, then run the test again. Keep running the tests and making changes to your function until all the tests pass.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In conclusion, TDD approach is better than just jumping in the code because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It defeats the need for debugging. This is because you already laid down how you want your code to behave in the tests, you just write code to follow that behavior. This is the strong point of TDD.&lt;/li&gt;
&lt;li&gt;It makes you think about small chunks of code at a time. This is required in functional programming.&lt;/li&gt;
&lt;li&gt;It improves productivity because developers have to focus on &lt;em&gt;that&lt;/em&gt; part of code they are working on.&lt;/li&gt;
&lt;li&gt;You do not need manual testing to know your code works.&lt;/li&gt;
&lt;li&gt;One important advantage of TDD to me is how it helps in Behavior driven development (BDD).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some people argue that TDD wastes time, maybe they are right, but to me, the advantages listed above outweigh this argument and I can say for a fact that it does not waste time, especially when you have your test setup ready from onset.&lt;/p&gt;

&lt;p&gt;Thank you for reading. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>testing</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
