Removing all the elements from the List in C#

CsharpServer Side ProgrammingProgramming

<p>To remove all the elements from the List, the code is as follows &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/sFJoPSmf" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">using System; using System.Collections.Generic; public class Demo { &nbsp; &nbsp;public static void Main(String[] args) { &nbsp; &nbsp; &nbsp; List&lt;String&gt; list1 = new List&lt;String&gt;(); &nbsp; &nbsp; &nbsp; list1.Add(&quot;One&quot;); &nbsp; &nbsp; &nbsp; list1.Add(&quot;Two&quot;); &nbsp; &nbsp; &nbsp; list1.Add(&quot;Three&quot;); &nbsp; &nbsp; &nbsp; list1.Add(&quot;Four&quot;); &nbsp; &nbsp; &nbsp; list1.Add(&quot;Five&quot;); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Elements in List1...&quot;); &nbsp; &nbsp; &nbsp; foreach (string res in list1) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(res); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; List&lt;String&gt; list2 = new List&lt;String&gt;(); &nbsp; &nbsp; &nbsp; list2.Add(&quot;India&quot;); &nbsp; &nbsp; &nbsp; list2.Add(&quot;US&quot;); &nbsp; &nbsp; &nbsp; list2.Add(&quot;UK&quot;); &nbsp; &nbsp; &nbsp; list2.Add(&quot;Canada&quot;); &nbsp; &nbsp; &nbsp; list2.Add(&quot;Poland&quot;); &nbsp; &nbsp; &nbsp; list2.Add(&quot;Netherlands&quot;); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Elements in List2...&quot;); &nbsp; &nbsp; &nbsp; foreach (string res in list2) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(res); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot; Is List2 equal to List1? = &quot;+list2.Equals(list1)); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot; Count of elements in list2 = &quot;+list2.Count); &nbsp; &nbsp; &nbsp; list2.Clear(); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot; Count of elements in list2 (updated) = &quot;+list2.Count); &nbsp; &nbsp;} }</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">Elements&nbsp;in&nbsp;List1... One Two Three Four Five Elements&nbsp;in&nbsp;List2... India US UK Canada Poland Netherlands Is List2 equal to List1? = False Count of elements in list2 = 6 Count&nbsp;of&nbsp;elements&nbsp;in&nbsp;list2&nbsp;(updated)&nbsp;=&nbsp;0</pre><h2>Example</h2><p>Let us see another example &minus;</p><p><a class="demo" href="http://tpcg.io/bl415BaO" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">using System; using System.Collections.Generic; public class Demo { &nbsp; &nbsp;public static void Main(String[] args) { &nbsp; &nbsp; &nbsp; List&lt;String&gt; list = new List&lt;String&gt;(); &nbsp; &nbsp; &nbsp; list.Add(&quot;One&quot;); &nbsp; &nbsp; &nbsp; list.Add(&quot;Two&quot;); &nbsp; &nbsp; &nbsp; list.Add(&quot;Three&quot;); &nbsp; &nbsp; &nbsp; list.Add(&quot;Four&quot;); &nbsp; &nbsp; &nbsp; list.Add(&quot;Five&quot;); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Elements in List1...&quot;); &nbsp; &nbsp; &nbsp; foreach (string res in list) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(res); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot; Count of elements in list = &quot;+list.Count); &nbsp; &nbsp; &nbsp; list.Clear(); &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot; Count of elements in list (updated) = &quot;+list.Count); &nbsp; &nbsp;} }</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">Elements&nbsp;in&nbsp;List1... One Two Three Four Five Count of elements in list = 5 Count&nbsp;of&nbsp;elements&nbsp;in&nbsp;list&nbsp;(updated)&nbsp;=&nbsp;0</pre>
raja
Updated on 10-Dec-2019 07:05:10

Advertisements