<?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: Ajeeth thangarasu</title>
    <description>The latest articles on DEV Community by Ajeeth thangarasu (@ajeetht).</description>
    <link>https://dev.to/ajeetht</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%2F445150%2Ffa4e8a2b-555c-459f-bf4c-7da52f8a6eff.png</url>
      <title>DEV Community: Ajeeth thangarasu</title>
      <link>https://dev.to/ajeetht</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajeetht"/>
    <language>en</language>
    <item>
      <title>Introduction to C# delegate and multicast delegate</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Tue, 16 Feb 2021 02:22:40 +0000</pubDate>
      <link>https://dev.to/ajeetht/introduction-to-c-delegate-and-multicast-delegate-4fpk</link>
      <guid>https://dev.to/ajeetht/introduction-to-c-delegate-and-multicast-delegate-4fpk</guid>
      <description>&lt;h2&gt;What is a C# delegate?&lt;/h2&gt;

&lt;p&gt;The C# delegate is a reference data type that defines the method and their signature. The delegate stores the address of one or more methods. When a delegate for a method assigned, it behaves exactly like the method. It can be invoked like any other method with parameters and return value.&lt;br&gt;A delegate simply stores the reference of the methods with the same signatures such as return type and parameter list. A delegate can be invoked as like a class by creating an object for the delegate. The delegate object allows running the methods at run time without invoking it directly.&lt;/p&gt;

&lt;p&gt;This post was originally posted on &lt;a href="https://developersdoors.xyz"&gt;DevelopersDoors&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Usage of delegate:&lt;/h2&gt;

&lt;p&gt;When an event needs to be used then delegates can be used. Multicast delegates can be used for invoking many events at a time. When a class need to have more than one implementation of the method and to encapsulate a static method.&lt;/p&gt;

&lt;p&gt;Refer for when to use delegates and interfaces -&amp;gt; &lt;a href="https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms173173(v=vs.100)" rel="noreferrer noopener"&gt;https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms173173(v=vs.100)&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Declaring a delegate:&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;System.Delegate&lt;/code&gt; class is the base class for the delegate declaration. The delegate keyword is used to declare a delegate. The return type and parameters should same as the methods.&lt;/p&gt;

&lt;p&gt;Eg:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public delegate int CalDelegate(int num1,int num2);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Instantiation and invoking delegates:&lt;/h2&gt;

&lt;p&gt;To instantiate a delegate, an instance is created similar to how a class instance is created. When a delegate is instantiated, the address of the method is stored in the instance.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;Eg:&lt;br&gt;&lt;code&gt;Calculator cal=new Calculator();&lt;br&gt;CalDelegate sumDel=new CalDelegate(cal.sum);&lt;br&gt;CalDelegate mulDel=new CalDelegate(cal.multiply);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;code&gt;CalDelegate&lt;/code&gt; is the delegate created and the &lt;code&gt;sumDel&lt;/code&gt; is the delegate instance instantiated to&lt;code&gt; CalDelegate&lt;/code&gt; with the reference to the &lt;code&gt;sum()&lt;/code&gt; method in cal instance and &lt;code&gt;mulDel&lt;/code&gt; is for reference of &lt;code&gt;multiply()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;To invoke the delegate, calling the delegate instance with the parameter list referenced by the delegate with the same data type.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;Eg:&lt;br&gt;&lt;code&gt;sumDel(10, 12);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mulDel(10,12);&lt;/code&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;using&lt;/span&gt; &lt;span class="nn"&gt;System&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;delegate&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;CalDelegate&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;num1&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;num2&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;Calculator&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="nf"&gt;sum&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;num1&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;num2&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;num1&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;num2&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="nf"&gt;multiply&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;num1&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;num2&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;num1&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="n"&gt;num2&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;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Calculator&lt;/span&gt; &lt;span class="n"&gt;cal&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;Calculator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;CalDelegate&lt;/span&gt; &lt;span class="n"&gt;sumDel&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;CalDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// sumDel for sum() reference&lt;/span&gt;
        &lt;span class="n"&gt;CalDelegate&lt;/span&gt; &lt;span class="n"&gt;mulDel&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;CalDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//mulDel for multiply() reference&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="nf"&gt;sumDel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;12&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="nf"&gt;mulDel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;12&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;Output:&lt;/p&gt;

&lt;p&gt;22&lt;/p&gt;

&lt;p&gt;120&lt;/p&gt;

&lt;h2&gt;Multicast delegates:&lt;/h2&gt;

&lt;p&gt;To have a reference of more than one method in delegate multicast delegates is used. This mainly used in the multi-event invocation. The += operator is used to add the methods referred to delegate and -= operator is used to remove the methods. The methods are called in First-In-First-Out(FIFO) order.&lt;/p&gt;

&lt;p&gt;Remember when using multicast delegates use void as return types because the use of multiple methods will clash in return type. So void is safe to avoid runtime exception.&lt;/p&gt;

&lt;p&gt;Eg:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CalDelegate simpleDel=new CalDelegate(cal.sum);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;simpleDel += new CalDelegate(cal.multiply);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;simpleDel -= new CalDelegate(cal.sum);&lt;/code&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;using&lt;/span&gt; &lt;span class="nn"&gt;System&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;delegate&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CalDelegate&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;num1&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;num2&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;Calculator&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sum&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;num1&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;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;multiply&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;num1&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;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="n"&gt;num2&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;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Calculator&lt;/span&gt; &lt;span class="n"&gt;cal&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;Calculator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;CalDelegate&lt;/span&gt; &lt;span class="n"&gt;simpleDel&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;CalDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;simpleDel&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;CalDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//multicasting using += operator&lt;/span&gt;
        &lt;span class="nf"&gt;simpleDel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;12&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;Output:&lt;/p&gt;

&lt;p&gt;22&lt;/p&gt;

&lt;p&gt;120&lt;/p&gt;

&lt;p&gt;The above delegate example is used with multicast delegates in which the first&lt;code&gt;sum()&lt;/code&gt;method is referenced to the&lt;code&gt;simpleDel&lt;/code&gt;delegate. Then&lt;code&gt;multiply()&lt;/code&gt;method is added to multicast using &lt;code&gt;+=&lt;/code&gt; operator. To remove the methods from delegate simply use &lt;code&gt;-=&lt;/code&gt; operator. As said multicast delegate run in FIFO, first&lt;code&gt;sum()&lt;/code&gt;delegate reference is run and then&lt;code&gt;multiply()&lt;/code&gt;with output 22 and 120 respectively.&lt;/p&gt;

&lt;p&gt;For documentation of delegates refer -&amp;gt; &lt;a href="https://docs.microsoft.com/en-in/dotnet/api/system.delegate?view=netcore-3.1" rel="noreferrer noopener"&gt;https://docs.microsoft.com/en-in/dotnet/api/system.delegate?view=netcore-3.1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In next post, I will explain synchronous delegate, asynchronous delegate and covariance and contravariance delegates.&lt;/p&gt;

&lt;p&gt;For my other C# archives -&amp;gt; &lt;a href="https://developersdoors.xyz/category/programming/c/" rel="noreferrer noopener"&gt;Go&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you enjoy my post please share it and if any suggestion please comment on it.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to create C# List&lt;T&gt; generics</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Fri, 05 Feb 2021 10:22:06 +0000</pubDate>
      <link>https://dev.to/ajeetht/how-to-create-c-list-t-generics-hkk</link>
      <guid>https://dev.to/ajeetht/how-to-create-c-list-t-generics-hkk</guid>
      <description>&lt;h2&gt;C# List&amp;lt;T&amp;gt; generics&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;C# List&amp;lt;T&amp;gt; is a strongly typed collection for creating a list of objects of type &amp;lt;T&amp;gt;. &lt;/li&gt;
&lt;li&gt;It is similar to ArrayList, we can add, remove, search, sort, manipulate list-objects.&lt;/li&gt;
&lt;li&gt;In C#, ArrayList is a non-generic collection whereas List&amp;lt;T&amp;gt; is a generic collection.&lt;/li&gt;
&lt;li&gt;List&amp;lt;T&amp;gt; has high performance than ArrayList and type-safe. &lt;/li&gt;
&lt;li&gt;List&amp;lt;T&amp;gt; has similar methods and properties as the ArrayList.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Creating a C# List&amp;lt;T&amp;gt;&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;List&amp;lt;T&amp;gt; is created by using a new operator either it can be empty capacity or specify the size.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Syntax:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List&amp;lt;T&amp;gt; &amp;lt;list_name&amp;gt; = new List&amp;lt;T&amp;gt;(&amp;lt;capacity&amp;gt;);&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt; is the type of object to be created like &lt;code&gt;int, float, double&lt;/code&gt; etc.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;list_name&amp;gt;&lt;/code&gt; name of the list.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;capacity&amp;gt;&lt;/code&gt; is the size of the list which is optional.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List&amp;lt;string&amp;gt; List1 = new List&amp;lt;string&amp;gt;();&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Adding object to List&amp;lt;T&amp;gt;&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;Add(T obj) method will add the element type &amp;lt;T&amp;gt; in the list on by one.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List1.Add(“Element1”);&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;
&lt;code&gt;Insert(int index, T obj)&lt;/code&gt; method will add the element at the specified index.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List1.Insert(1,”Element1”);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Removing object to List&amp;lt;T&amp;gt;&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;
&lt;code&gt;Remove(T obj)&lt;/code&gt; method will remove the element in the list.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List1.Remove(“Element1”);&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;
&lt;code&gt;RemoveAt(int index)&lt;/code&gt; Removes the element at the specified index.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List1.RemoveAt(1);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Get the size of List&amp;lt;T&amp;gt;:&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;
&lt;code&gt;Count&lt;/code&gt; property is used to get the size of the list.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List1.Count();&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;To sort the List&amp;lt;T&amp;gt;:&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;Use the &lt;code&gt;Sort()&lt;/code&gt; method to sort the array in ascending order.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List1.Sort();&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Accessing the elements in the List&amp;lt;T&amp;gt;&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;The index number can be used to access the elements like an array.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;List1[0] // accessing element at index 0;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;To iterate over the List&amp;lt;T&amp;gt;, use foreach function.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Eg:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;foreach(string str in List1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Console.WriteLine(str);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;Example:&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&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;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//create new List&amp;lt;T&amp;gt; of string type using new operator.&lt;/span&gt;
        &lt;span class="n"&gt;List&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="n"&gt;list1&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;List&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="c1"&gt;//Adding elements using Add(T) method.&lt;/span&gt;
        &lt;span class="n"&gt;list1&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="s"&gt;"Element1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;list1&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="s"&gt;"Element2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;list1&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="s"&gt;"Element3"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;list1&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="s"&gt;"Element4"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;list1&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="s"&gt;"Element5"&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;"Printing elements in list1"&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;"Size of list1: "&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;list1&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="c1"&gt;//prinitng size of list1 using count&lt;/span&gt;

        &lt;span class="c1"&gt;//printing list1 elements using foreach&lt;/span&gt;
        &lt;span class="k"&gt;foreach&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;str&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;//removing 'Element1' 'Element2' using Remove(T) method.&lt;/span&gt;
        &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element2"&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;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;"Printing elements in list1 after removing Element1, Element2"&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;"Size of list1: "&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;list1&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="c1"&gt;//prinitng size of list1 using count&lt;/span&gt;

        &lt;span class="c1"&gt;//printing list1 elements using foreach&lt;/span&gt;
        &lt;span class="k"&gt;foreach&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;str&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Insert&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="s"&gt;"Element1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//inserting 'Element1' in index 2&lt;/span&gt;
        &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Insert&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="s"&gt;"Element2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//inserting 'Element2' in index 4&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;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;"Printing elements in list1 after inserting Element1, Element2"&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;"Size of list1: "&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;list1&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="c1"&gt;//prinitng size of list1 using count&lt;/span&gt;
        &lt;span class="c1"&gt;//printing list1 elements using foreach&lt;/span&gt;
        &lt;span class="k"&gt;foreach&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;str&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Sort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="c1"&gt;//sorting list1&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;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;"Printing elements in list1 after sorting"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;//printing list1 elements using foreach&lt;/span&gt;
        &lt;span class="k"&gt;foreach&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;str&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;Output:&lt;/h3&gt;

&lt;pre&gt;Printing elements in list1
Size of list1: &lt;span&gt;5&lt;/span&gt;
Element1
Element2
Element3
Element4
Element5

Printing elements in list1 after removing Element1, Element2
Size of list1: &lt;span&gt;3&lt;/span&gt;
Element3
Element4
Element5

Printing elements in list1 after inserting Element1, Element2
Size of list1: &lt;span&gt;5&lt;/span&gt;
Element3
Element4
Element1
Element5
Element2

Printing elements in list1 after sorting
Element1
Element2
Element3
Element4
Element5&lt;/pre&gt;

&lt;p&gt;It is recommended to use List&amp;lt;T&amp;gt; generic over ArrayList since it is type-safe, high performance. &lt;/p&gt;

&lt;p&gt;To more refer:&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0#remarks"&gt;https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0#remarks&lt;/a&gt;&lt;/p&gt;








&lt;p&gt;For my other programming archives -&amp;gt; &lt;a href="https://developersdoors.xyz/category/programming/"&gt;Go&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If any suggestions please comment about it.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>C# interface and multiple interface tutorial</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Tue, 02 Feb 2021 11:25:25 +0000</pubDate>
      <link>https://dev.to/ajeetht/c-interface-and-multiple-interface-tutorial-ni8</link>
      <guid>https://dev.to/ajeetht/c-interface-and-multiple-interface-tutorial-ni8</guid>
      <description>&lt;h2&gt;What is interface?&lt;/h2&gt;

&lt;p&gt;The definitions for a group of related functionalities that a non-abstract class or struct can declare is called an interface. The interface is another way of abstraction in C#. The interface is like the abstract class but it only contains the methods definitions, whereas the abstract class contains all methods, fields, constants definitions. Unlike the abstract class, the interface cannot have a constructor. The interface methods should be abstract and public. The interface only provides the definition for the methods. In C# multiple inheritances cannot be done in class but by means of the interface.&lt;/p&gt;

&lt;h2&gt;The interface in C#:&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Members in the interface should be abstract and public only no other access specifier used.&lt;/li&gt;
&lt;li&gt;By interface, we can’t able to create an object like an abstract class.&lt;/li&gt;
&lt;li&gt;Interface only define the methods, the class needed to override and implement those methods.&lt;/li&gt;
&lt;li&gt;The interface doesn’t contain a constructor.&lt;/li&gt;
&lt;li&gt;The interface contains methods and properties but not fields.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Implementation of C# interface:&lt;/h2&gt;

&lt;p&gt;The interface keyword used to implement the interface. We can also implement multiple interfaces for a single class in C#. It's suggested to use I before every interface name to identify it has the interface. To use an interface in a class we use (:) to access the interface. For example, demo as interface name then use &lt;code&gt;IDemo&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Syntax:&lt;/h2&gt;

&lt;pre&gt;&lt;span&gt;interface&lt;/span&gt; &amp;lt;&lt;span&gt;interface_name&lt;/span&gt;&amp;gt;
{
    method definition;
}
&lt;span&gt;class&lt;/span&gt; &amp;lt;&lt;span&gt;class_name&lt;/span&gt;&amp;gt;:&amp;lt;&lt;span&gt;interface_name&lt;/span&gt;&amp;gt;
{
interface_method declaration;
}&lt;/pre&gt;

&lt;h2&gt;Example:&lt;/h2&gt;

&lt;pre&gt;&lt;span&gt;using&lt;/span&gt; System;
    
&lt;span&gt;interface&lt;/span&gt; &lt;span&gt;IDemo1&lt;/span&gt;
{
    &lt;span&gt;&lt;span&gt;void&lt;/span&gt; &lt;span&gt;simpleInterface&lt;/span&gt;()&lt;/span&gt;;
}

&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Demo&lt;/span&gt;:&lt;span&gt;IDemo1&lt;/span&gt;{
    &lt;span&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; &lt;span&gt;simpleInterface&lt;/span&gt;()&lt;/span&gt;{
        Console.WriteLine(&lt;span&gt;"Implementation of Idemo1 interface"&lt;/span&gt;);
    }
}
&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Program&lt;/span&gt;
{
    &lt;span&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; &lt;span&gt;Main&lt;/span&gt;()
    &lt;/span&gt;{
        Demo d=&lt;span&gt;new&lt;/span&gt; Demo();
        d.simpleInterface();
        Console.ReadLine();
    }
}&lt;/pre&gt;

&lt;p&gt;Here I implement an interface called &lt;code&gt;IDemo1&lt;/code&gt;. Then I use the &lt;code&gt;IDemo1&lt;/code&gt; interface in &lt;code&gt;Demo&lt;/code&gt; class using the operator &lt;code&gt;(:)&lt;/code&gt;. Then I implement the interface method &lt;code&gt;simpleInterface&lt;/code&gt; by declaring an output statement which displays Implementation IDemo1 interface.&lt;/p&gt;

&lt;h2&gt;Multiple interfaces:&lt;/h2&gt;

&lt;p&gt;To use multiple interfaces, simply use a comma between the interfaces name in class. The above program can be implemented as multiple interfaces with another interface called &lt;code&gt;Idemo2&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;using&lt;/span&gt; System;
    
&lt;span&gt;interface&lt;/span&gt; &lt;span&gt;IDemo1&lt;/span&gt;
{
    &lt;span&gt;&lt;span&gt;void&lt;/span&gt; &lt;span&gt;simpleInterface1&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;)&lt;/span&gt;;
}

&lt;span&gt;interface&lt;/span&gt; &lt;span&gt;IDemo2&lt;/span&gt;
{
    &lt;span&gt;&lt;span&gt;void&lt;/span&gt; &lt;span&gt;simpleInterface2&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;)&lt;/span&gt;;
}
&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Demo&lt;/span&gt;:&lt;span&gt;IDemo1&lt;/span&gt;,&lt;span&gt;IDemo2&lt;/span&gt;{
    &lt;span&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; &lt;span&gt;simpleInterface1&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;)&lt;/span&gt;{
        Console.WriteLine(&lt;span&gt;"Implementation of Idemo1 interface"&lt;/span&gt;);
    }
    
    &lt;span&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; &lt;span&gt;simpleInterface2&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;)&lt;/span&gt;{
        Console.WriteLine(&lt;span&gt;"Implementation of Idemo2 interface"&lt;/span&gt;);
    }
}
&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Program&lt;/span&gt;
{
    &lt;span&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; &lt;span&gt;Main&lt;/span&gt;(&lt;span&gt;&lt;/span&gt;)
    &lt;/span&gt;{
        Demo d=&lt;span&gt;new&lt;/span&gt; Demo();
        d.simpleInterface1();
        d.simpleInterface2();
        Console.ReadLine();
    }
}&lt;/pre&gt;

&lt;p&gt;From &lt;code&gt;C# 8.0&lt;/code&gt; onwards we can implement a default implementation for the interface methods itself. We can also use other access specifiers such as &lt;code&gt;private, protected, internal, public, virtual, abstract, override, sealed, static, extern&lt;/code&gt;. We can also implement static methods in interfaces. &lt;/p&gt;

&lt;p&gt;For reference &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions"&gt;https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For my other programming post -&amp;gt; &lt;a href="https://developersdoors.xyz/category/programming/"&gt;Go&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>A simple jquery calculator for beginners</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Mon, 04 Jan 2021 04:23:32 +0000</pubDate>
      <link>https://dev.to/ajeetht/a-simple-jquery-calculator-for-beginners-52p7</link>
      <guid>https://dev.to/ajeetht/a-simple-jquery-calculator-for-beginners-52p7</guid>
      <description>&lt;p&gt;I created a post on &lt;a href="http://developersdoors.xyz/a-simple-jquery-calculator-application/"&gt;http://developersdoors.xyz/a-simple-jquery-calculator-application/&lt;/a&gt; kindly refer it for the explanation.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/ajeetht/embed/RwaBOVR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is hyperledger fabric blockchain?</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Sat, 02 Jan 2021 13:20:45 +0000</pubDate>
      <link>https://dev.to/ajeetht/what-is-hyperledger-fabric-blockchain-1j3i</link>
      <guid>https://dev.to/ajeetht/what-is-hyperledger-fabric-blockchain-1j3i</guid>
      <description>&lt;p&gt; &lt;a href="https://www.hyperledger.org/projects/fabric" rel="noopener noreferrer"&gt;&lt;span&gt;Hyperledger Fabric&lt;/span&gt;&lt;/a&gt;&lt;span&gt; is an open-source enterprise-grade permissioned distributed ledger technology platform. Focus for use in enterprise contexts. It has key features than any other blockchain platforms. I will explain what is a blockchain and how it is useful. Then I explain about Hyperledger Fabric blockchain platform. &lt;/span&gt;&lt;/p&gt;

&lt;h2&gt;What is a blockchain?&lt;/h2&gt;

&lt;p&gt;Blockchain is an immutable chain of records called a ledger. A hash function associated with the ledger and stored in chronological order of execution. These records may be a transaction done between two nodes. Blockchain network is a distributed decentralized network, where each peer nodes have a copy of the ledger. Since blockchain is a decentralized network, there is no centralized host. the reliability achieved through &lt;a href="https://en.wikipedia.org/wiki/Consensus_(computer_science)" rel="noopener noreferrer"&gt;consensus protocol&lt;/a&gt;. Each node needs to verify and validate the transaction in the network. This feature makes blockchain with high security.&lt;/p&gt;

&lt;p&gt;This is how a blockchain works. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.investopedia.com%2Fthmb%2FOspmuCTSRyVEcNwSLOwYGaQHm64%3D%2F1688x0%2Ffilters%3Ano_upscale%28%29%3Amax_bytes%28150000%29%3Astrip_icc%28%29%3Aformat%28webp%29%2Fdotdash_Final_Blockchain_Sep_2020-01-60f31a638c4944abbcfde92e1a408a30.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.investopedia.com%2Fthmb%2FOspmuCTSRyVEcNwSLOwYGaQHm64%3D%2F1688x0%2Ffilters%3Ano_upscale%28%29%3Amax_bytes%28150000%29%3Astrip_icc%28%29%3Aformat%28webp%29%2Fdotdash_Final_Blockchain_Sep_2020-01-60f31a638c4944abbcfde92e1a408a30.jpg" alt="What is hyperleder fabric blochchain?-blockchain"&gt;&lt;/a&gt;Source: &lt;a href="https://www.investopedia.com/terms/b/blockchain.asp" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://www.investopedia.com/terms/b/blockchain.asp" rel="noopener noreferrer"&gt;https://www.investopedia.com/terms/b/blockchain.asp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each time a hash function for the block created and connected with the previous block. This forms a chain of cryptographic records. After each transaction, the nodes in each node in the network store the copy of that ledger. This makes blockchain a fault tolerance network.&lt;/p&gt;

&lt;p&gt;For more reference &lt;a href="https://www.investopedia.com/terms/b/blockchain.asp" rel="noopener noreferrer"&gt;https://www.investopedia.com/terms/b/blockchain.asp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Bitcoin" rel="noopener noreferrer"&gt;Bitcoin&lt;/a&gt; by &lt;a href="https://en.wikipedia.org/wiki/Satoshi_Nakamoto" rel="noopener noreferrer"&gt;Satoshi Nakamoto&lt;/a&gt; is the first blockchain network used for cryptocurrency. Nowadays, blockchain technology has many uses cases like banking, hospitality, supply chain, government etc.&lt;/p&gt;

&lt;h3&gt;some of the interesting use cases are:&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Deutsche Bahn, the second-largest transport company in world and infrastructure management, digitizing the railway transport with blockchain for assets management, railway signalling infrastructure. To more &lt;a href="https://www.deutschebahn.com/en/Digitalization/technology/New-Technology/blockchain-3520362" rel="noopener noreferrer"&gt;read here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Walmart manages its food supply chain in an unpredicted way using hyperledger fabric blockchain network. To more &lt;a href="https://www.hyperledger.org/learn/publications/walmart-case-study" rel="noopener noreferrer"&gt;read here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.burstiq.com/" rel="noreferrer noopener"&gt;BurstIQ’s&lt;/a&gt; platform helps healthcare companies to safe and secure massive amounts of patient data. Its blockchain technology enables the safekeeping, sale, sharing or license of data while maintaining strict compliance with HIPAA rules.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are many blockchain networks available. These can be divided into two categories.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Public blockchain: &lt;/strong&gt;Public blockchain is where any participants can be able to participate in the blockchain network and make a transaction between them. &lt;strong&gt;Bitcoin, Ethereum&lt;/strong&gt; which are the&lt;strong&gt; &lt;/strong&gt;most popular public blockchain network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private blockchain:&lt;/strong&gt; Private blockchain main focus is for B2B. A consortium of organizations or organizations under governance use this blockchain. Private blockchain archives high privacy and security. The most popular private blockchain is &lt;strong&gt;Hyperledger technologies such as fabric and sawtooth, Corda, Ripple, Ethereum&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;What is hyperledger fabric blockchain?&lt;/h2&gt;

&lt;p&gt;As said Hyperledger Fabric is a distributed ledger platform. The main focus for enterprise private blockchain solution. It is an open-source project under Linux-foundation started by IBM. It is an active project in Hyperledger technologies. Currently, more than 30 companies contribute to Hyperledger fabric.&lt;/p&gt;

&lt;p&gt;Hyperledger technologies have different frameworks and libraries like fabric, sawtooth, indy, besu. It also has tools for managing blockchain networks. For more details go to &lt;a href="http://Hyperledger%20technologies%20have%20different%20frameworks%20and%20libraries%20like%20fabric,%20sawtooth,%20indy,%20besu.%20It%20also%20has%20tools%20for%20managing%20blockchain%20networks.%20For%20more%20details%20go%20to%20hyperledger.org" rel="noopener noreferrer"&gt;hyperledger.org&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Why hyperledger fabric?&lt;/h2&gt;

&lt;p&gt;Hyperledger fabric focus in private permissioned blockchain. It has many key features than other blockchain platforms to standalone.&lt;/p&gt;

&lt;p&gt;They key features are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modularity&lt;/li&gt;
&lt;li&gt;Permissioned&lt;/li&gt;
&lt;li&gt;Privacy and confidentiality&lt;/li&gt;
&lt;li&gt;Performance and scalability&lt;/li&gt;
&lt;li&gt;Smart contract in form of chaincode&lt;/li&gt;
&lt;li&gt;Pluggable consensus&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Modularity&lt;/h3&gt;

&lt;p&gt;Hyperledger Fabric has a modular architecture. The modularity of consensus protocol, cryptographic functions, ease of smart contract development, key management protocol and different algorithms.&lt;/p&gt;

&lt;p&gt;To know how hyperledger works go to &lt;a href="https://hyperledger-fabric.readthedocs.io/en/release-2.2/txflow.html" rel="noopener noreferrer"&gt;https://hyperledger-fabric.readthedocs.io/en/release-2.2/txflow.html&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Permissioned&lt;/h3&gt;

&lt;p&gt;Hyperledger fabric is a private blockchain. The main key focus is on privacy and security. A blockchain platform for the enterprise can be fully deployed using this framework. This use different consensus protocol such as byzantine and crash fault tolerance protocols.&lt;/p&gt;

&lt;p&gt;Additionally, in such a permissioned context, the risk of a participant intentionally introducing malicious code through a smart contract is diminished. First, the participants are known to one another and all actions, whether submitting application transactions, modifying the configuration of the network or deploying a smart contract recorded on the blockchain following an endorsement policy that was established for the network and relevant transaction type.&lt;/p&gt;

&lt;h3&gt;Privacy and confidentiality&lt;/h3&gt;

&lt;p&gt;Hyperledger Fabric, being a permissioned platform, enables confidentiality through its channel architecture and private data feature. In channels, participants on a Fabric network establish a sub-network where every member has visibility to a particular set of transactions. Thus, only those nodes that participate in a channel have access to the smart contract (chaincode) and data transacted, preserving the privacy and confidentiality of both. Private data allows collections between members on a channel, allowing much of the same protection as channels without the maintenance overhead of creating and maintaining a separate channel.&lt;/p&gt;

&lt;h3&gt;Performance and scalability&lt;/h3&gt;

&lt;p&gt;In performance and scalability, Hyperledger Fabric standalone from other blockchain platforms. Variables such as transaction size, block size, network size, as well as limits of the hardware, etc may affect the performance. The Hyperledger Fabric &lt;a href="https://wiki.hyperledger.org/display/PSWG/Performance+and+Scale+Working+Group" rel="noopener noreferrer"&gt;Performance and Scale working group&lt;/a&gt; currently working on a benchmarking framework called &lt;a href="https://wiki.hyperledger.org/projects/caliper" rel="noopener noreferrer"&gt;Hyperledger Caliper&lt;/a&gt;. The latest &lt;a href="https://arxiv.org/abs/1901.00910" rel="noopener noreferrer"&gt;scaled Fabric to 20,000 transactions per second&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Fabric introduces a new architecture for transactions that we call &lt;strong&gt;execute-order-validate&lt;/strong&gt;. It addresses the resiliency, flexibility, scalability, performance and confidentiality challenges faced by the order-execute model by separating the transaction flow into three steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;execute&lt;/em&gt; a transaction and check its correctness, thereby endorsing it,&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;order&lt;/em&gt; transactions via a (pluggable) consensus protocol, and&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;validate&lt;/em&gt; transactions against an application-specific endorsement policy before committing them to the ledger&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This design departs radically from the order-execute paradigm in that Fabric executes transactions before reaching a final agreement on their order.&lt;/p&gt;

&lt;p&gt;We can deploy those blockchain network using docker container technology also. Thus scalability of the blockchain network is efficient than other platforms. Many cloud service providers such as AWS, Azure, IBM provide cloud blockchain solution. Where we can easily deploy the blockchain network and make ease of transaction in it.&lt;/p&gt;

&lt;h3&gt;Smart contract in form of chaincode&lt;/h3&gt;

&lt;p&gt;A smart contract, or what Fabric calls “chaincode”, functions as a trusted distributed application that gains its security/trust from the blockchain and the underlying consensus among the peers. It is the business logic of a blockchain application.&lt;br&gt;There are three key points that apply to smart contracts, especially when applied to a platform:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;many smart contracts run concurrently in the network,&lt;/li&gt;&lt;/ul&gt;

&lt;ul&gt;&lt;li&gt;A smart contract can be dynamically deployed&lt;/li&gt;&lt;/ul&gt;

&lt;ul&gt;&lt;li&gt;Untrusted and potentially malicious code treated as well.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;It is easy to write that chaincode in native languages such as NodeJS, JAVA, Go, Typescript. No need to have separate language for smart contract.&lt;/p&gt;

&lt;h3&gt;Pluggable consensus&lt;/h3&gt;

&lt;p&gt;A consensus algorithm is a procedure through which all the peers of the Blockchain network reach a common agreement about the present state of the distributed ledger. In this way, consensus algorithms achieve reliability in the Blockchain network and establish trust between unknown peers in a distributed computing environment. Essentially, the consensus protocol makes sure that every new block added to the Blockchain is the one and only version of the truth that agreed upon by all the nodes in the Blockchain.&lt;/p&gt;

&lt;p&gt;Hyperledger fabric uses two types of the consensus protocol. one is Byzantine protocol and another one is crash fault tolerance. As of modular architecture, it uses them as for the problem in the blockchain network. Thus achieve more reliability to the blockchain network.&lt;/p&gt;

&lt;p&gt;For complete features visit &lt;a href="https://hyperledger-fabric.readthedocs.io/en/release-2.2/whatis.html" rel="noopener noreferrer"&gt;https://hyperledger-fabric.readthedocs.io/en/release-2.2/whatis.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I learned about this technology for my college project. It is an awesome open source project. You can easily develop a smart contract with NodeJS. We can deploy the blockchain network using containers also. For more go to &lt;a href="https://hyperledger-fabric.readthedocs.io" rel="noopener noreferrer"&gt;https://hyperledger-fabric.readthedocs.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I will be posting articles on hyperledger fabric administration and chaincode development. If you have any suggestion or corrections in this article please share in the comments.&lt;/p&gt;

&lt;p&gt;For my other programming archives -&amp;gt; &lt;a href="https://developersdoors.xyz/category/programming/" rel="noopener noreferrer"&gt;Go&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>hyperledgerfabric</category>
      <category>introduction</category>
    </item>
    <item>
      <title>What is docker networking and bridge networking</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Fri, 01 Jan 2021 08:48:18 +0000</pubDate>
      <link>https://dev.to/ajeetht/what-is-docker-networking-and-bridge-networking-11gc</link>
      <guid>https://dev.to/ajeetht/what-is-docker-networking-and-bridge-networking-11gc</guid>
      <description>&lt;p&gt;In docker we can connect two or more containers or non-docker system using networking. Whether it is a windows or Linux containers, docker network helps to connect them in an easy way. This may helpful for beginners who interested in docker.&lt;/p&gt;

&lt;p&gt;This post was originally posted on &lt;a href="http://www.developersdoors.xyz" rel="noopener noreferrer"&gt;www.developersdoors.xyz&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;There are different network drivers available in docker networking&lt;/h2&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;code&gt;bridge&lt;/code&gt;:
The default network driver if we don't mention a network driver when running a container. It is created automatically when a container is created. It is best for standalone containers&lt;/li&gt;
    &lt;li&gt;
&lt;code&gt;host&lt;/code&gt;:
If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host, and the container does not get its own IP-address allocated.
For instance, if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.&lt;/li&gt;
    &lt;li&gt;
&lt;code&gt;overlay&lt;/code&gt;:
Overlay is mainly used when we want to connect multiple containers to create a swarm service to communicate between them.&lt;/li&gt;
    &lt;li&gt;
&lt;code&gt;macvlan&lt;/code&gt;:
Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses.&lt;/li&gt;
    &lt;li&gt;The other driver is called &lt;code&gt;none&lt;/code&gt;, which is used when we use third party plugins from docker hub.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Bridge networking tutorial&lt;/h2&gt;

&lt;p&gt;This tutorial explain how a two standalone containers can be connected by bridge network. Here we create two &lt;code&gt;alpine&lt;/code&gt; linux containers namely alpine1 and alphine2, then connect them using bridge.&lt;br&gt;
We can view the network list using the command&lt;/p&gt;

&lt;pre&gt;docker network ls&lt;/pre&gt;

&lt;p&gt;You may see different network listed as &lt;code&gt;bridge&lt;/code&gt;, &lt;code&gt;host&lt;/code&gt;, &lt;code&gt;none&lt;/code&gt; as following image. Any newly created network also listed here.&lt;br&gt;
&lt;a href="http://developersdoors.xyz/wp-content/uploads/2020/12/network_ls.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdevelopersdoors.xyz%2Fwp-content%2Fuploads%2F2020%2F12%2Fnetwork_ls-1024x408.png" alt="Docker_network_ls"&gt;&lt;/a&gt;&lt;br&gt;
Next create two alpine containers as alpine 1 and alpine 2 with following command&lt;/p&gt;

&lt;pre&gt;docker run -dit --name alpine1 alpine 
docker run -dit --name alpine2 alpine&lt;/pre&gt;

&lt;p&gt;The two container created as follows&lt;br&gt;
&lt;a href="http://developersdoors.xyz/wp-content/uploads/2020/12/contianer_create.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdevelopersdoors.xyz%2Fwp-content%2Fuploads%2F2020%2F12%2Fcontianer_create-1024x386.png" alt="container_create"&gt;&lt;/a&gt;&lt;br&gt;
Then inspect the bridge network to view details of the containers connected to it with the following command&lt;/p&gt;

&lt;pre&gt;docker network inspect bridge&lt;/pre&gt;

&lt;p&gt;This will display a JSON format of bridge network details. We could see that by default alpine1 and alpine2 has bridge networks with different IP address as &lt;code&gt;172.17.0.2/16&lt;/code&gt; and &lt;code&gt;172.17.0.3/16&lt;/code&gt; respectively .&lt;br&gt;
&lt;a href="http://developersdoors.xyz/wp-content/uploads/2020/12/network_inspect-2.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdevelopersdoors.xyz%2Fwp-content%2Fuploads%2F2020%2F12%2Fnetwork_inspect-2-853x1024.png" alt="network_inspect "&gt;&lt;/a&gt;&lt;br&gt;
Now connect to the alpine1 container using &lt;code&gt;attach&lt;/code&gt; command&lt;/p&gt;

&lt;pre&gt;docker attach alpine1&lt;/pre&gt;

&lt;p&gt;Now attach to the container and ping a website say google.com &lt;code&gt;-c 2&lt;/code&gt; make 2 limit of ping.&lt;br&gt;
&lt;a href="http://developersdoors.xyz/wp-content/uploads/2020/12/ping-google.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdevelopersdoors.xyz%2Fwp-content%2Fuploads%2F2020%2F12%2Fping-google-1024x615.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
Then try to ping the alpine2 container using the IP &lt;code&gt;172.17.0.3/16&lt;/code&gt; and check they are connected. If it ping correctly then successfully two containers are connected using bridge.&lt;/p&gt;

&lt;pre&gt;ping -c 2 172.17.0.3/16&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://developersdoors.xyz/wp-content/uploads/2020/12/ping-alpine2.png" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdevelopersdoors.xyz%2Fwp-content%2Fuploads%2F2020%2F12%2Fping-alpine2-1024x571.png" alt="docker_networking_bridge"&gt;&lt;/a&gt;&lt;br&gt;
For other docker archive -&amp;gt; &lt;a href="https://developersdoors.xyz/category/programming/docker/" rel="noopener noreferrer"&gt;go&lt;/a&gt;.&lt;br&gt;
For network reference of docker -&amp;gt; &lt;a href="https://docs.docker.com/network/" rel="noopener noreferrer"&gt;go&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>networking</category>
      <category>tutorial</category>
      <category>devops</category>
    </item>
    <item>
      <title>How to restart containers automatically in docker</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Mon, 28 Dec 2020 05:33:09 +0000</pubDate>
      <link>https://dev.to/ajeetht/how-to-restart-containers-automatically-in-docker-4hb6</link>
      <guid>https://dev.to/ajeetht/how-to-restart-containers-automatically-in-docker-4hb6</guid>
      <description>&lt;p&gt;If we are using docker containers for personal use we can restart it manually using docker restart. The docker restart command allow us to restart containers with some time limit. But, in production it is difficult to restart by manual. To restart containers automatically, we can use restart policies for docker containers.&lt;/p&gt;

&lt;p&gt;This post was originally posted on &lt;a href="https://developersdoors.xyz/how-to-restart-containers-automatically-in-docker/" rel="noopener noreferrer"&gt;developersdoors&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Restart policies for docker containers&lt;/h2&gt;

&lt;p&gt;Restart polies allows the container to restart automatically in required situations. The situations may be when a failure occurs, when daemon starts, when a container stopped.&lt;br&gt;The &lt;code&gt;--restart&lt;/code&gt; flag is used with docker run command when starting a container. There are four restart policies available.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;no&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Do not automatically restart containers anyway&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;always&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Always restart the container even though the container stopped manually. Until the daemon stops or the restart policy is changed, it will restart it in loop.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;on-failure&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restart the container if it exits due to an error, which manifests as a non-zero exit code. If any container is stopped due to any fault, it will automatically restart those containers with this policy.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;unless-stopped&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Similar to &lt;code&gt;always&lt;/code&gt;, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To run a container with restart policies, try with following code pattern&lt;/p&gt;

&lt;pre&gt;docker run --restart no hello-world&lt;/pre&gt;

&lt;p&gt;The above command run the &lt;code&gt;hello-world&lt;/code&gt; image with restart policy set as &lt;code&gt;no&lt;/code&gt;. It will not restart the containers automatically. &lt;/p&gt;

&lt;pre&gt;docker update --restart always hello-world&lt;/pre&gt;

&lt;p&gt;Here I use &lt;code&gt;update&lt;/code&gt; command to update the restart policy of the hello-world container with &lt;code&gt;always&lt;/code&gt; policy. This will restart the container always even though it is stopped manually or start when the daemon starts.&lt;/p&gt;

&lt;p&gt;To view the events that occurs during restart we can use event command.&lt;/p&gt;

&lt;pre&gt;docker event&lt;/pre&gt;

&lt;p&gt;Open the docker event in one shell and run the container with always policy with another shell. The event shows the hello-world container will restart automatically every times it stopped. The image shows that every time the hello-world container stopped, it restart it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdevelopersdoors.xyz%2Fwp-content%2Fuploads%2F2020%2F12%2Fdocker_events-693x1024.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fdevelopersdoors.xyz%2Fwp-content%2Fuploads%2F2020%2F12%2Fdocker_events-693x1024.png" alt="docker restart containers(1)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also restart the containers using process managers such as upstart, systemd, supervisor. I will be posting an article on using process managers for docker in later. &lt;/p&gt;

&lt;p&gt;For any reference to docker -&amp;gt; &lt;a href="https://docs.docker.com/" rel="noopener noreferrer"&gt;go&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For my other docker archives -&amp;gt;&lt;a href="https://developersdoors.xyz/category/programming/docker/" rel="noopener noreferrer"&gt; go&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you like the article feel free to share.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The difference between COPY and ADD in dockerfile</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Sun, 13 Dec 2020 14:55:32 +0000</pubDate>
      <link>https://dev.to/ajeetht/the-difference-between-copy-and-add-in-dockerfile-3llp</link>
      <guid>https://dev.to/ajeetht/the-difference-between-copy-and-add-in-dockerfile-3llp</guid>
      <description>&lt;p&gt;When I was seeing some examples for docker file image building, I came across two things of same functionalities. They are COPY and ADD instructions. This article gives the difference between ADD and COPY in dockerfile. Next it explains how similar they are, then the best practice for using the RUN instead of ADD instruction.&lt;/p&gt;

&lt;h3&gt;ADD instruction&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ADD&lt;/code&gt; instruction is an older one, which job is to copy the file or directory from a source to destination.&lt;br&gt;
The &lt;code&gt;ADD&lt;/code&gt; instruction can also do operations such as extraction of compressed files or download from an URL path.&lt;br&gt;
Here the source may be a local compressed tar file or the URL path.&lt;br&gt;
If it is a tar file, it extracts the contents to the destination else if it is an URL, then it download the file and extract to the destination in a similar way.&lt;br&gt;
If authentication needs for URL file we can use &lt;code&gt;RUN&lt;/code&gt; with &lt;code&gt;curl&lt;/code&gt; or &lt;code&gt;wget&lt;/code&gt; to download the files.&lt;br&gt;
Since, &lt;code&gt;ADD&lt;/code&gt; degrades in performance of the docker containers, &lt;code&gt;COPY&lt;/code&gt; is been introduced for simple job.&lt;br&gt;
&lt;u&gt;Syntax for ADD:&lt;/u&gt;&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;ADD&lt;/span&gt; &lt;span&gt;&amp;lt;src&amp;gt; &amp;lt;dest&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;A simple example to ADD used for local tar file called source.tar.xz to the destination folder /dest.&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;ADD&lt;/span&gt; &lt;span&gt;sourcefile.tar.xz /dest&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This syntax example gives how the URL path called &lt;a href="https://example.com/source.tar.xz"&gt;https://example.com/source.tar.xz&lt;/a&gt; file is downloaded and copied to the /dest.&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;ADD&lt;/span&gt; &lt;span&gt;https://example.com/source.tar.xz /dest&lt;/span&gt;&lt;/pre&gt;

&lt;h3&gt;COPY instruction&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;COPY&lt;/code&gt; instruction copies the file or directory from a source path to the destination.&lt;br&gt;
Its job is simple as it duplicates the source file or directory to the destination.&lt;br&gt;
It doesn't include the operation of extraction or downloading files as &lt;code&gt;ADD&lt;/code&gt; instruction.&lt;br&gt;
&lt;u&gt;Syntax for COPY:&lt;/u&gt;&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;COPY&lt;/span&gt; &lt;span&gt;&amp;lt;src&amp;gt; &amp;lt;dest&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;An example to COPY a file called source.txt to the destination folder /dest.&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;COPY&lt;/span&gt; &lt;span&gt;sourcefile.txt /dest&lt;/span&gt;&lt;/pre&gt;

&lt;h3&gt;What to use either COPY or ADD?&lt;/h3&gt;

&lt;p&gt;In dockerfile we use &lt;code&gt;COPY&lt;/code&gt; many time because it only copies the file from a source to destination.&lt;code&gt;ADD&lt;/code&gt; used when there is a purpose such as local tar files or URL file source.&lt;br&gt;
For best docker practice, we can use &lt;code&gt;RUN&lt;/code&gt; instruction instead of creating an extra layer.&lt;br&gt;
&lt;code&gt;RUN&lt;/code&gt; instruction with &lt;code&gt;curl&lt;/code&gt; or &lt;code&gt;wget&lt;/code&gt; used to get the file direct into the destination.&lt;br&gt;
The &lt;code&gt;RUN&lt;/code&gt; instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;ADD&lt;/span&gt; &lt;span&gt;https://example.com/source.tar.xz /usr/src/things/
&lt;/span&gt;&lt;span&gt;RUN&lt;/span&gt; &lt;span&gt;tar -xJf /usr/src/things/source.tar.xz -C /usr/src/things
&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;For instance take the above sentence, where a file called source.tar.gz is downloaded using &lt;code&gt;ADD&lt;/code&gt; and extracted using tar with &lt;code&gt;RUN&lt;/code&gt; instruction.&lt;br&gt;
This makes two layers that worse the docker performance.&lt;br&gt;
It simply done using &lt;code&gt;RUN&lt;/code&gt; with curl tool as follows within a single layer.&lt;br&gt;
If, there is direct copy operation needed than &lt;code&gt;COPY&lt;/code&gt; instruction is recommended.&lt;br&gt;
The below dockerfile only takes single layer of operation of file download using curl and then extraction of file using tar with &lt;code&gt;RUN&lt;/code&gt; instruction.&lt;/p&gt;

&lt;pre&gt;&lt;span&gt;RUN&lt;/span&gt; &lt;span&gt;mkdir -p /usr/src/things \
    &amp;amp;&amp;amp; curl -SL https://example.com/source.tar.xz \
    | tar -xJC /usr/src/things &lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Reference to the Best docker practices -&amp;gt; &lt;a href="https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#add-or-copy"&gt;go&lt;/a&gt;.&lt;br&gt;
For my docker archives click here -&amp;gt; &lt;a href="https://developersdoors.xyz/category/programming/docker/"&gt;go&lt;/a&gt;.&lt;br&gt;
If any queries please comment it.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>tutorial</category>
      <category>linux</category>
    </item>
    <item>
      <title>Deploy a React application using docker containers</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Thu, 12 Nov 2020 01:15:46 +0000</pubDate>
      <link>https://dev.to/ajeetht/deploy-a-react-application-using-docker-containers-2ln3</link>
      <guid>https://dev.to/ajeetht/deploy-a-react-application-using-docker-containers-2ln3</guid>
      <description>&lt;p&gt;We can deploy a react application in production easily in docker my means of node and nginx docker images. Take a 5 mins read of &lt;a href="http://www.developersdoors.xyz/deploy-a-react-application-using-docker-containers/"&gt;www.developersdoors.xyz/deploy-a-react-application-using-docker-containers/&lt;/a&gt; which helps to deploy a react application in docker.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Docker introduction and working</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Thu, 08 Oct 2020 15:35:45 +0000</pubDate>
      <link>https://dev.to/ajeetht/docker-introduction-and-working-ghj</link>
      <guid>https://dev.to/ajeetht/docker-introduction-and-working-ghj</guid>
      <description>&lt;p&gt;Docker is an open source platform to develop, ship and run an application in an isolated way using docker containers. Learn about docker containers, images and how efficiently docker works from 5 mins read of &lt;a href="https://developersdoors.xyz/docker-containers/"&gt;https://developersdoors.xyz/docker-containers/&lt;/a&gt; &lt;/p&gt;

</description>
      <category>docker</category>
      <category>tutorial</category>
      <category>devops</category>
    </item>
    <item>
      <title>Developers doors</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Tue, 25 Aug 2020 14:13:43 +0000</pubDate>
      <link>https://dev.to/ajeetht/developers-doors-4bd0</link>
      <guid>https://dev.to/ajeetht/developers-doors-4bd0</guid>
      <description>&lt;p&gt;My tech blog about programming tutorials and interesting tech news. Please support it and state your comments.&lt;br&gt;
&lt;a href="https://developersdoors.xyz/"&gt;https://developersdoors.xyz/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blog</category>
      <category>technology</category>
    </item>
    <item>
      <title>Merge conflicts and learn how to resolve it in git.</title>
      <dc:creator>Ajeeth thangarasu</dc:creator>
      <pubDate>Tue, 25 Aug 2020 14:10:47 +0000</pubDate>
      <link>https://dev.to/ajeetht/merge-conflicts-and-learn-how-to-resolve-it-in-git-3lil</link>
      <guid>https://dev.to/ajeetht/merge-conflicts-and-learn-how-to-resolve-it-in-git-3lil</guid>
      <description>&lt;p&gt;Merge conflicts occurs when we try to merge two branches of same files altered. This usually can occur between team members. Take a 5 mins read.&lt;br&gt;
&lt;a href="https://developersdoors.xyz/merge-conflicts-and-learn-how-to-resolve-it/"&gt;https://developersdoors.xyz/merge-conflicts-and-learn-how-to-resolve-it/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>merge</category>
      <category>conflicts</category>
      <category>github</category>
    </item>
  </channel>
</rss>
