Generics C#

Add and Remove Elements From a List

This C# program demonstrates how to add and remove elements from a list.

Program.cs

using System;
using System.Collections.Generic;


namespace XoaX {

    class Program {

        static bool EndsWithAH(string sString) {
            return sString.EndsWith("ah");
        }

        static void Main(string[] args) {
            // Create a list of Old Testament books
            List<string> qBooksOT = new List<string>();

            // Add books individually
            qBooksOT.Add("Ezekiel");
            qBooksOT.Add("Daniel");
            qBooksOT.Add("Hosea");
            qBooksOT.Add("Joel");
            qBooksOT.Add("Amos");
            qBooksOT.Add("Obadiah");
            qBooksOT.Add("Jonah");
            qBooksOT.Add("Micah");
            qBooksOT.Add("Nahum");

            // Display the initial list
            Console.WriteLine("Initial List:");
            Console.WriteLine("-------------");
            foreach (string sBook in qBooksOT) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");

            // Create a list of Old Testament books
            List<string> qSecondList = new List<string>();

            // Add books individually
            qSecondList.Add("Isaiah");
            qSecondList.Add("Jeremiah");
            qSecondList.Add("Lamentations");
            qSecondList.Add("Baruch");

            // Display the second list
            Console.WriteLine("Second List:");
            Console.WriteLine("------------");
            foreach (string sBook in qSecondList) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");

            // Add the second to the first
            qBooksOT.AddRange(qSecondList);

            // Display the combined list
            Console.WriteLine("After Add Range:");
            Console.WriteLine("----------------");
            foreach (string sBook in qBooksOT) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");

            // Remove the second through fifth elements
            qBooksOT.RemoveRange(1, 4);

            // Display the removed range list
            Console.WriteLine("Removed 2nd through 5th List:");
            Console.WriteLine("-----------------------------");
            foreach (string sBook in qBooksOT) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");


            qBooksOT.RemoveAt(6);

            // Display the removed 7th element list
            Console.WriteLine("Removed 7th List:");
            Console.WriteLine("-----------------");
            foreach (string sBook in qBooksOT) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");

            qBooksOT.Remove("Isaiah");

            // Display the removed "Isaiah" element list
            Console.WriteLine("Removed Isaiah List:");
            Console.WriteLine("--------------------");
            foreach (string sBook in qBooksOT) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");

            qBooksOT.RemoveAll(EndsWithAH);

            // Display the removed "ah" element list
            Console.WriteLine("Removed ah List:");
            Console.WriteLine("----------------");
            foreach (string sBook in qBooksOT) {
                Console.WriteLine(sBook);
            }
            Console.WriteLine("");
        }

    }

}
 

Output

Initial List:
-------------
Ezekiel
Daniel
Hosea
Joel
Amos
Obadiah
Jonah
Micah
Nahum

Second List:
------------
Isaiah
Jeremiah
Lamentations
Baruch

After Add Range:
----------------
Ezekiel
Daniel
Hosea
Joel
Amos
Obadiah
Jonah
Micah
Nahum
Isaiah
Jeremiah
Lamentations
Baruch

Removed 2nd through 5th List:
-----------------------------
Ezekiel
Obadiah
Jonah
Micah
Nahum
Isaiah
Jeremiah
Lamentations
Baruch

Removed 7th List:
-----------------
Ezekiel
Obadiah
Jonah
Micah
Nahum
Isaiah
Lamentations
Baruch

Removed Isaiah List:
--------------------
Ezekiel
Obadiah
Jonah
Micah
Nahum
Lamentations
Baruch

Removed ah List:
----------------
Ezekiel
Nahum
Lamentations
Baruch

Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.