Fundamentals of Collections

Introduction

A collection, sometimes referred to as a list, is a group of items. The items can be numbers, characters, strings, or objects. In fact, a collection can be a list of other lists. In computer systems and application development, a collection is extremely helpful because it allows you to treat many values or objects as one unit, instead of always treating each individually. This ability makes many operations possible in areas of databases, communication (streaming), file processing (including storage), etc.

Practical LearningPractical Learning: Introducing Collections

  1. Start Microsoft Visual Studio
  2. Create a Console App project named PayrollPreparation8 and that supports .NET 8.0 (Long-Term Support)

A Collection from an Array

The C# language by itself doesn't have a library for collections. The type of collection you can create in C# is an array, which is a type of collection but with some fundamental limitations (that can be overcome). Therefore, to start a simple collection in C#, you can create an array as we have seen in previous lessons. Here is an example:

int[] numbers = { 15, 928, 731_495, 80, 749};

In the same way, we have seen that you can create an array of objects.

Visual Basic to the Rescue

Introduction

When you create an array, the compiler requires that you let it know the number of items your array will have. This means that an array is a fixed collection of items. After creating an array, you cannot add more items than you had originally indicated (the .NET Framework, not the C# language, provides the ability, or rather a technique, or a mechanism, to grow an array). This is not an anomaly. This is the way arrays are defined in C-based languages (C++, Java, and C#). In some scenarios or applications, you want a collection that can grow and srink at will (the .NET Framework provides all types of classes to create and manage any type of collection).

One of the ways you can create a collection is to borrow it from the Visual Basic language. Before doing that, you must "import" the Visual Basic library into your application.

Practical LearningPractical Learning: Importing the Visual Basic Library

  1. In the Solution Explorer, right-click the PayrollPreparation8 project and click Manage NuGet Packages...
  2. In the NuGet: PayrollPreparation8 tab, click Browse
  3. Click in the combo box below browse and type VisualBasic :

    NuGet Package Management

  4. Click Install
  5. On the Preview Changes dialog box, click Apply:

    Preview Changes

Starting a Collection

To support collections, the Visual Basic language provides a class named Collection. After importing the Visual Basic library into your project, you can start by declaring a VisualBasic.Collection variable. Normally, if you are working in Microsoft Visual Studio and you have imported the Visual Basic library, as soon as you type Collection and press Enter or you press the Space bar, the studio will automatically add the Visual Basic namespace in the top section of your document. Otherwise, you can add that namespace manually or you will have to qualify every instance of Collection in your document. Here is an example:

using Microsoft.VisualBasic;

Collection numbers;

Creating a Collection from a List

If you have a list of items you want to create as a group of items, put the items between an opening square bracket ([) and a closing square bracket (]), separate the items with comas, and assign that list of the VisualBasic.Collection variable. You must end the statement with the traditional semicolon. Here is an example that creates a collection of integers:

using Microsoft.VisualBasic;

Collection numbers = [ 15, 928, 731_495, 80, 749 ];

In the same way, you can create a simple collection of strings. Here is an example:

using Microsoft.VisualBasic;

Console.WriteLine("Hello, World!");

Collection distributions = [ "Linux Mint", "openSUSE", "Arch Linux", "Ubuntu", "FreeBSD" ];

You can also use this technique to create a collection of objects. To do that, in the above placeholders where we were putting simple values, you can define the desired object. Remember to separate them with comas. Here is an example of a collection of objects:

using Microsoft.VisualBasic;
using static System.Console;

Collection states =
    [
        new State() { Abbreviation = "WA",  StateName = "Western Australia", AreaSqrKms = 2529875, Capital = "Perth"     },
        new State() { Abbreviation = "SA",  StateName = "South Australia",   AreaSqrKms =  983482, Capital = "Adelaide"  },
        new State() { Abbreviation = "QLD", StateName = "Queensland",        AreaSqrKms = 1730648, Capital = "Brisbane"  },
        new State() { Abbreviation = "NSW", StateName = "New South Wales",   AreaSqrKms =  800642, Capital = "Sydney"    },
        new State() { Abbreviation = "VIC", StateName = "Victoria",          AreaSqrKms =  227416, Capital = "Melbourne" },
        new State() { Abbreviation = "TAS", StateName = "Tasmania",          AreaSqrKms =   68401, Capital = "Hobart"    }
    ];

internal interface IAbbreviated
{
    string? Abbreviation { get; set; }
}
public abstract class GovernmentEntity
{
    public virtual string? StateName { get; set; }
    public virtual int AreaSqrKms { get; set; }
    public virtual string? Capital { get; set; }
}
public class State : GovernmentEntity,
                        IAbbreviated
{
    public string? Abbreviation { get; set; }
}

Adding an Item to a Collection

The most fundamental operation to perform on a collection is to add an item to it. To support this operation, the VisualBasic.Collection class provides a method named Add. Its syntax is:

public void Add(object? Item,
                string? Key    = default,
                object? Before = default,
                object? After  = default);

This method takes four arguments but only one is required. The required argument is the value or object to add to the VisualBasic.Collection collection. Since the argument is an object object, it can be anything, such as a number. Just like you can add one value or object, you can keep calling the VisualBasic.Collection.Add() method to add values one after another. Here are examples:

using Microsoft.VisualBasic;

Collection numbers = new Collection();

numbers.Add(15);
numbers.Add(928);
numbers.Add(731_495);
numbers.Add(80);
numbers.Add(749);

One of the advantages of a collection over an array is that, at any time, you can add a new item to a collection. To do that, simply call the VisualBasic.Collection.Add() method and pass the desired new item.

Adding a Collection of Items

If you decide to use the VisualBasic.Collection.Add() method to add items, if you have many items to add, remember that you have to call that method each time. If you already know the items to add, after the new() or the new class-name() expression, type the opening curly bracket and closing curly bracket. In those brackets, create a list of the items. Separate them with comas. Of course, you must end the statement with a semicolon. Here is an example:

using Microsoft.VisualBasic;

Collection numbers = new Collection() { 15, 928, 731_495, 80, 749 };

Adding a List to a List

Remember that the item to add to a VisualBasic.Collection collection is an object. As a result, the item can be a list to add to the list. Here are examples:

using Microsoft.VisualBasic;

Collection odds = [1, 3, 5, 7, 9 ];
Collection evens = [2, 4, 6, 8];

Collection numbers = new Collection();

numbers.Add(0);
numbers.Add(odds);
numbers.Add(58_280);
numbers.Add(evens);
numbers.Add(10);

Maintaining the Items of a Collection

Introduction

Data maintenance consists a locating records, editing, and deleting them. To assist you with such operations, the C# language provides some techniques, but most collection classes provide their own tools through methods.

The Number of Items in a Collection

One of the most valuable characteristics of a list is the number of items it has. To provide this information, the VisualBasic.Collection class is equipped with a read-only property named Count:

public int Count { get; }

The VisualBasic.Collection.Count is an integer that represents the position of an item in the list. Once you have that item, you can perform various types of operations.

Accessing the Items of a Collection

After adding items to a collection, the second most important operation to perform is to maintain those items. The operations that involve data maintenance start from locating an item. To assist you with this operation, the VisualBasic.Collection class is equipped an overloaded indexed property. Once of the versions takes an integer. Its syntax is:

public object? this[int Index] { get; }

With this property, you can use a loop (for, while, or do...while). Apply the square brackets to the name of the collection, and pass the desired index to the square brackets. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection numbers = new Collection() { 15, 928, 731_495, 80, 749 };

WriteLine("Numbers");
WriteLine("----------------");

int i = 1;

// Loop
while(i <= numbers.Count)
{
    WriteLine("Number: {0}", numbers[i]);
    ++i;
}

WriteLine("================");

This would produce:

Numbers
----------------
Number: 15
Number: 928
Number: 731495
Number: 80
Number: 749
================

Press any key to close this window . . .

The VisualBasic.Collection class provides one more version of its indexed property:

public object? this[object Index] { get; }

Checking Whether a Collection Contains a Certain Item

One of the most routine operations to perform on a list is to locate an item. Many techniques are available. You can create a function to perform that operation. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

_ = Analyze();

int Analyze()
{
    Collection counties = new Collection()
    {
        "Bristol",
        "Kent",
        "Newport",
        "Providence",
        "Washington"
    };

    Write("Enter the name of a county in Rhode Island: ");
    string strCounty = ReadLine()!;

    WriteLine("----------------------------------------------------");

    if (Contains(counties, strCounty) == true)
    {
        WriteLine("\"{0}\" is one of the counties in Rhode Island.", strCounty);
    }
    else
    {
        WriteLine("There is no county named \"{0}\" in Rhode Island.", strCounty);
    }

    WriteLine("====================================================");

    return 1_000;
}

bool Contains(Collection coll, object item)
{
    int i = 1;

    while (i <= coll.Count)
    {
        if (coll[i]!.ToString()!.Equals(item) )
        {
            return true;
        }

        ++i;
    }

    return false;
}

Here is an example of running the application:

nter the name of a county in Rhode Island: Newport
----------------------------------------------------
"Newport" is one of the counties in Rhode Island.
====================================================

Press any key to close this window . . .

Here is another example of running the application:

Enter the name of a county in Rhode Island: News Port
----------------------------------------------------
There is no county named "News Port" in Rhode Island.
====================================================

Press any key to close this window . . .

Iterating Through a Collection

As a normal list layout, the VisualBasic.Collection class is equipped with a method named GetEnumerator. Its syntax is:

public System.Collections.IEnumerator GetEnumerator ();

This method returns an IEnumerator object. As a result, this method makes it possible to use a foreach loop to visit each item of a list. Here is an example of doing that:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    "January", "February", "March",
    "April",   "May",      "June",
    "July",    "August",   "September",
    "October", "November", "December", 
};

WriteLine("Calendar");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}

WriteLine("===================");

This produce:

Calendar
-------------------
Month:  January
Month:  February
Month:  March
Month:  April
Month:  May
Month:  June
Month:  July
Month:  August
Month:  September
Month:  October
Month:  November
Month:  December
===================

Press any key to close this window . . .

We saw that, since the VisualBasic.Collection.Add() method takes an object object, you can pass a list to it as argument. If you do, you can use a foreach loop to visit each item of the list passed as argument. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection odds  = [1, 3, 5, 7, 9];
Collection evens = [2, 4, 6, 8];

Collection numbers = new Collection();

numbers.Add(0);
numbers.Add(odds);
numbers.Add(58_280);
numbers.Add(evens);
numbers.Add(10);

WriteLine("Numbers");
WriteLine("---------------");

foreach (var item in numbers)
{
    if (item is Collection colls)
    {
        foreach (var coll in colls)
        {
            WriteLine("Number: {0}", coll);
        }
    }
    else
    {
        WriteLine("Number: {0}", item);
    }
}

WriteLine("===============");

This would produce:

Numbers
---------------
Number: 0
Number: 1
Number: 3
Number: 5
Number: 7
Number: 9
Number: 58280
Number: 2
Number: 4
Number: 6
Number: 8
Number: 10
===============

Press any key to close this window . . .

Deleting Items from a Collection

Removing an Item from a List

The final common operation to perform on a list consists of deleting an item. To assist you with this operation, the VisualBasic.Collection class is equipped with an oveloaded method named Remove.

To let you delete an item based on its position, the VisualBasic.Collection class provides the following version of its Remove() method:

public void Remove (int Index);

When calling this method, pass a natural number that represents the index of the item you want to remove. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
};

WriteLine("Calendar");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

months.Remove(4);

WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}

WriteLine("===================");

This would produce:

Calendar
-------------------
Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================
-------------------
Month: January
Month: February
Month: March
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================

Press any key to close this window . . .

When calling the VisualBasic.Collection.Remove() method, if you pass 0, a negative number, or a number that is higher than the VisualBasic.Collection.Count value of the collection, the compiler would throw an System.IndexOutOfRangeException exception. Of course, if you are anticipating this type of situation, you can create a conditional statement. This can be done as follows:

using Microsoft.VisualBasic;
using static System.Console;

int index = -4;

Collection months = new()
{
    "January", "February", "March",
    "April",   "May",      "June",
    "July",    "August",   "September",
    "October", "November", "December",
};

if ((index is <= 0) | (index is > 12))
{
    WriteLine("You must provide a valid index for the item you want to remove.");
}
else
{
    months.Remove(index);

    WriteLine("Calendar");
    WriteLine("-------------------");

    foreach (var month in months)
    {
        WriteLine("Month: {0}", month);
    }

    WriteLine("===================");
}

Clearing a Collection

Consider the following code:

using Microsoft.VisualBasic;
using static System.Console;

Collection counties = new Collection()
{
    "Bristol",
    "Kent",
    "Newport",
    "Providence",
    "Washington"
};

int i = 1;

WriteLine("Rhode Island");
WriteLine("------------------");

while (i <= counties.Count)
{
    WriteLine("County: {0}", counties[i]);
    
    ++i;
}
WriteLine("==================");

This would produce:

Rhode Island
------------------
County: Bristol
County: Kent
County: Newport
County: Providence
County: Washington
==================

Press any key to close this window . . .

Clearing a colection consissts of deleting all its items, as opposed to removing one item at a time. To assist you with this operation, the VisualBasic.Collection class provides a method named Clear. Its syntax is:

public void Clear();

To remove all the items from a list, you can simply call this method on the VisualBasic.Collection variable. This can be done as follows:

using Microsoft.VisualBasic;
using static System.Console;

Collection counties = new Collection()
{
    "Bristol",
    "Kent",
    "Newport",
    "Providence",
    "Washington"
};

counties.Clear();

int i = 1;

WriteLine("Rhode Island");
WriteLine("------------------");

while (i <= counties.Count)
{
    WriteLine("County: {0}", counties[i]);
    
    ++i;
}
WriteLine("==================");

When calling the VisualBasic.Collection.Clear() method, if the collection is empty, nothing would happen. If the collection contains at least one item, it would be emptied. The above code would produce:

Rhode Island
------------------
==================

Press any key to close this window . . .

An Empty Collection

A collection is referred to as empty if it doesn't contain any item. You have various options to get en empty collection.

As the simplest way to have an empty collection, you can declare a VisualBasic.Collection variable and initialize it with its default constructor. This can be done as follows:

using Microsoft.VisualBasic;

Collection months = new();

When a collection is empty, its Count property is equal to 0.

After declaring a simple VisualBasic.Collection variable, if you want to populate it, you can call or keep calling, the Add() method to add items to the list.

We already saw how to delete some items from a collection. In the same way, you can keep calling the VisualBasic.Collection.Remove() method to remove more items. If the last item is deleted, the list becomes empty. That's another way to get an empty list.

If you call the VisualBasic.Collection.Clear() method to delete all items from a list, the list becomes empty. That's one more way to get an empty list.

Introduction to Dictionaries

Overview

A dictionary is a collection of items where each item is a combination of a key and a value, or a value and a key. This means that each item in fact contains two values.

Creating a Dictionary

A Visual Basic dictionary starts like any collection. This means that, to create a dictionary, start by declaring a VisualBasic.Collection variable. Then, to add an item to the dictionary, call the VisualBasic.Collection.Add() method. Remember that this method takes an object argument, which means the argument can be anything. Based on this, to add a dictionary item to the collection, pass two values (or an object and a value) to the method. The second item should be passed as a string. The first part can be anything you want, such as a number, a string, an object, etc. Here is an example of a dictionary where each item is a combination of two strings:

using Microsoft.VisualBasic;

Collection states = new Collection();

states.Add("Western Australia", "WA");
states.Add("South Australia", "SA");
states.Add("Queensland", "QLD");
states.Add("New South Wales", "NSW");
states.Add("Victoria", "VIC");
states.Add("Tasmania", "TAS");

Accessing the Items of a Dictionary

To assist you with accessing the items of a dictionary, the VisualBasic.Collection class is equipped with the following version of its indexed property:

public object? this[string Key] { get; }

Using this version of the indexed property, pass the argument as the key, which is the second argument to the Add() method. When you do that, the property produces the value or object corresponding to the key of the same item, that value or object is the one that was passed as the first part of the argument of the Add() method. Here are examples:

using Microsoft.VisualBasic;
using static System.Console;

Collection states = new();

states.Add("Western Australia", "WA");
states.Add("South Australia",   "SA");
states.Add("Queensland",        "QLD");
states.Add("New South Wales",   "NSW");
states.Add("Victoria",          "VIC");
states.Add("Tasmania",          "TAS");

WriteLine("Australia");
WriteLine("--------------------------");
WriteLine("State:  {0}", states["WA"]);
WriteLine("State:  {0}", states["SA"]);
WriteLine("State:  {0}", states["QLD"]);
WriteLine("State:  {0}", states["NSW"]);
WriteLine("State:  {0}", states["VIC"]);
WriteLine("State:  {0}", states["TAS"]);
WriteLine("==========================");

This would produce:

Australia
--------------------------
State:  Western Australia
State:  South Australia
State:  Queensland
State:  New South Wales
State:  Victoria
State:  Tasmania
==========================

Press any key to close this window . . .

An important rule of a dictionary is that each key must be unique. In your dictionary, if you create two items that have the same key, when you access the dictionary, the compiler will throw an System.ArgumentException exception. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    { "January",   "Jan" },
    { "February",  "Feb" },
    { "March",     "Mar" },
    { "April",     "Apr" },
    { "May",       "May" },
    { "June",      "Jun" },
    { "July",      "Jul" },
    { "August",    "Aug" },
    { "September", "Feb" },
    { "October",   "oct" },
    { "November",  "Nov" },
    { "December",  "Dec" },
};

WriteLine("Month: {0}", months["Sep"]);

This would produce:

Unhandled exception. System.ArgumentException: Add failed. Duplicate key value supplied.
   at Microsoft.VisualBasic.Collection.Add(Object Item, String Key, Object Before, Object After)
   at Program.<Main>$(String[] args) in E:\Users\pkatts\source\repos\Consoles\Exercise1\Exercise1\Exercise.cs:line 4

Press any key to close this window . . .

Dictionary Maintenance

Iterating Through a Dictionary

Remember that the VisualBasic.Collection class is equipped with a GetEnumerator() method that returns an IEnumerator object. This allows you to use a foreach loop to visit each item of the dictionary. If you use a foreach loop on a dictionary, each section would produce the value of the item. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection states = new Collection
{
    { "Western Australia", "WA" },
    { "South Australia", "SA" },
    { "Queensland", "QLD" },
    { "New South Wales", "NSW" },
    { "Victoria", "VIC" },
    { "Tasmania", "TAS" }
};

WriteLine("Australia");
WriteLine("--------------------------");

foreach(var state in states)
{
    WriteLine("State:  {0}", state);
}

WriteLine("==========================");

This would produce:

Australia
--------------------------
State:  Western Australia
State:  South Australia
State:  Queensland
State:  New South Wales
State:  Victoria
State:  Tasmania
==========================

Press any key to close this window . . .

Checking Whether a Collection Contains a Certain Item

To let you locate a value in a dictionary, the VisualBasic.Collection class is equipped with a Boolean method named Contains. Its syntax is:

public bool Contains (string Key);

This method takes one argument as the key of the item you are trying to find. The argument is passed as a string. When this method is called, the compiler will try to find an item that uses that key. If the item is found, the method returns true and you can get its value. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection states = new Collection
{
    { "Western Australia", "WA" },
    { "South Australia", "SA" },
    { "Queensland", "QLD" },
    { "New South Wales", "NSW" },
    { "Victoria", "VIC" },
    { "Tasmania", "TAS" }
};

WriteLine("Australia");
WriteLine("--------------------------");

if (states.Contains("NSW") == true)
{
    WriteLine("State:  {0}", states["NSW"]);
}

WriteLine("==========================");

This would produce:

Australia
--------------------------
State:  New South Wales
==========================

Press any key to close this window . . .

Removing an Item from a Dictionary

Remember that a dictionary is a collection of key/value items. After creating a dictionary, for your maintenance operations, you may be interested in removing an item. To perform this operation, you must know the key of the item you want to delete. To assist you with this operation, the VisualBasic.Collection classs provides the following version of its Remove() method:

public void Remove (string Key);

When calling this method, pass a string that represents the key of the item you want to remove. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    { "January",   "Jan" },
    { "February",  "Feb" },
    { "March",     "Mar" },
    { "April",     "Apr" },
    { "May",       "May" },
    { "June",      "Jun" },
    { "July",      "Jul" },
    { "August",    "Aug" },
    { "September", "Sep" },
    { "October",   "oct" },
    { "November",  "Nov" },
    { "December",  "Dec" },
};

months.Remove("Feb");

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

This would produce:

Months
-------------------
Month: January
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================

Press any key to close this window . . .

When calling the VisualBasic.Collection.Remove(string Key) method, if you pass a key that doesn't exist in the dictionary, the compiler would throw an System.ArgumentException exception. Of course, there are actions you can take to find out whether the dictionary contains the key contains that key.

Clearing a Collection

Consider the following code:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    { "January",   "Jan" },
    { "February",  "Feb" },
    { "March",     "Mar" },
    { "April",     "Apr" },
    { "May",       "May" },
    { "June",      "Jun" },
    { "July",      "Jul" },
    { "August",    "Aug" },
    { "September", "Sep" },
    { "October",   "oct" },
    { "November",  "Nov" },
    { "December",  "Dec" },
};

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

This would produce:

Months
-------------------
Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================
Months
-------------------
Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================

Press any key to close this window . . .

Clearing a collection consists of deleting all its items, as opposed to removing one item at a time. To assist you with this operation, the VisualBasic.Collection class provides a method named Clear. Its syntax is:

public void Clear();

To remove all the items of a dictionary, you can simply call this method on the VisualBasic.Collection variable. This can be done as follows:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    { "January",   "Jan" },
    { "February",  "Feb" },
    { "March",     "Mar" },
    { "April",     "Apr" },
    { "May",       "May" },
    { "June",      "Jun" },
    { "July",      "Jul" },
    { "August",    "Aug" },
    { "September", "Sep" },
    { "October",   "oct" },
    { "November",  "Nov" },
    { "December",  "Dec" },
};

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

months.Clear();

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

When calling the VisualBasic.Collection.Clear() method, if the collection is empty, nothing would happen. If the collection contains at least one item, it would be emptied. The above code would produce:

Months
-------------------
Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================
Months
-------------------
===================

Press any key to close this window . . .

Clearing a Collection

Consider the following code:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    { "January",   "Jan" },
    { "February",  "Feb" },
    { "March",     "Mar" },
    { "April",     "Apr" },
    { "May",       "May" },
    { "June",      "Jun" },
    { "July",      "Jul" },
    { "August",    "Aug" },
    { "September", "Sep" },
    { "October",   "oct" },
    { "November",  "Nov" },
    { "December",  "Dec" },
};

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

This would produce:

Months
-------------------
Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================
Months
-------------------
Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
===================

Press any key to close this window . . .

To delete all items from a dictionary, you can call the VisualBasic.Collection.Clear() method. Here is an example:

using Microsoft.VisualBasic;
using static System.Console;

Collection months = new()
{
    { "January",   "Jan" },
    { "February",  "Feb" },
    { "March",     "Mar" },
    { "April",     "Apr" },
    { "May",       "May" },
    { "June",      "Jun" },
    { "July",      "Jul" },
    { "August",    "Aug" },
    { "September", "Sep" },
    { "October",   "oct" },
    { "November",  "Nov" },
    { "December",  "Dec" },
};

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

months.Clear();

WriteLine("Months");
WriteLine("-------------------");

foreach (var month in months)
{
    WriteLine("Month: {0}", month);
}
WriteLine("===================");

Previous Copyright © 2008-2024, FunctionX Friday 26 November 2021 Home