List Extensions - BNS-MarkUlrich/MarksMagicToolbox GitHub Wiki

Overview

ListExtensions is a static class containing extension methods that enhance the functionality of lists in C#. These methods offer additional operations to check inclusion, retrieve filtered lists, and augment lists with new elements.

Installation

To use ListExtensions in your C# project, follow these steps:

  1. Copy the ListExtensions.cs file into your project's scripts directory.
  2. Ensure the System.Collections.Generic and UnityEngine namespaces are included.

Usage

Methods

Method Description Parameters
Contains<T>(this target, compareTarget) Checks if the compareTarget list includes all elements present in the target list. target: The source list to be checked.
compareTarget: The list being compared against the source list.
ContainsAny<T>(this target, compareTarget) Checks if the compareTarget list includes any element present in the target list. target: The source list to be checked.
compareTarget: The list being compared against the source list.
GetListWithout<T>(this list, excludeParams) Returns a new list without the specified elements from the original list. list: The original list.
excludeParams: The elements to exclude from the list.
GetListWithout<T>(this list, excludeList, excludeParams) Returns a new list that contains all elements from the input list, excluding elements from the excludeList and excludeParams. list: The original list.
excludeParams: The elements to exclude from the list.
excludeList: The list of elements to exclude.
AddRange<T>(this list, elements) Overloads the standard AddRange() method. Adds the elements of the specified array to the end of the list. list: The list to add the elements to.
elements: The elements to add to the list.

Example

using System.Collections.Generic;
using UnityEngine;

public class ListExtensionsExample : MonoBehaviour
{

    private void Start()
    {
        ExampleUsage();
    }

    public void ExampleUsage()
    {
        List<int> listA = new List<int> { 1, 2, 3, 4, 5 };
        List<int> listB = new List<int> { 2, 4 };

        bool containsAll = listA.Contains(listB); // Check if listA contains all elements in listB
        bool containsAny = listB.ContainsAny(listA); // Check if listA contains any element in listB

        if (containsAll)
            print("listA contains all elements in listB");
            
        if (containsAny) 
            print("listB contains any element in listA");

        listA.AddRange(6, 7, 8); // Add elements to listA

        List<int> listWithout = listA.GetListWithout(listB); // Get a list without elements from listB        

        // add all elements of ListA into single string
        string listAString = string.Join(", ", listA.ToArray()); // 1, 2, 3, 4, 5, 6, 7, 8
        print(listAString);

        // add all elements of ListWithout into single string
        string listWithoutString = string.Join(", ", listWithout.ToArray()); // 1, 3, 5, 6, 7, 8
        print(listWithoutString);
    }
}

Repository

Explore extensions for working with lists, checking inclusion relationships, and more.

⚠️ **GitHub.com Fallback** ⚠️