Skip to content

ArrayList Extensions

Andrzej Kebab edited this page Jan 21, 2024 · 1 revision

UtilityLibrary.Core

IsNullOrEmpty

Checks if the ArrayList is null or empty.

using UtilityLibrary.Core;
using System;
using System.Collections;

ArrayList list1 = new ArrayList();
ArrayList list2 = null;

bool isNullOrEmpty1 = list1.IsNullOrEmpty();
bool isNullOrEmpty2 = list2.IsNullOrEmpty();

// Result: isNullOrEmpty1 = true, isNullOrEmpty2 = true

IsEmpty

Checks if the ArrayList is empty, throwing an exception if it is null.

using UtilityLibrary.Core;
using System;
using System.Collections;

ArrayList list = new ArrayList();

bool isEmpty = list.IsEmpty();

// Result: isEmpty = true

First

Retrieves the first element of the ArrayList.

using UtilityLibrary.Core;
using System;
using System.Collections;

ArrayList list = new ArrayList { 1, 2, 3, 4, 5 };

object firstElement = list.First();

// Result: firstElement = 1

First (with type casting)

Retrieves the first element of the ArrayList and casts it to the specified type.

using UtilityLibrary.Core;
using System;
using System.Collections;

ArrayList list = new ArrayList { 1, 2, 3, 4, 5 };

int firstElement = list.First<int>();

// Result: firstElement = 1

Last

Retrieves the last element of the ArrayList.

using UtilityLibrary.Core;
using System;
using System.Collections;

ArrayList list = new ArrayList { 1, 2, 3, 4, 5 };

object lastElement = list.Last();

// Result: lastElement = 5

Last (with type casting)

Retrieves the last element of the ArrayList and casts it to the specified type.

using UtilityLibrary.Core;
using System;
using System.Collections;

ArrayList list = new ArrayList { 1, 2, 3, 4, 5 };

int lastElement = list.Last<int>();

// Result: lastElement = 5

Clone this wiki locally