Skip to content

Version 6.x.x SingleItem

Pawel Gerr edited this page Oct 8, 2023 · 1 revision

SingleItem (collections)

Resource-saving alternative for creation of a collection with 1 item only.

// We have to overloads of a method. One with 1 item only, and the the other using a collection.
// The one should call the other to prevent implementing the method twice.
public void Do(IReadOnlyCollection<string> names)
{
    // implemention
}

public void Do(string name)
{
    // Delegates the call to other overload.

    IReadOnlyList<string> names = SingleItem.Collection(name);
    
    Do(names);
}

------------

IReadOnlyList<string> list = SingleItem.Collection("name");

IReadOnlyDictionary<int, string> dictionary = SingleItem.Dictionary(42, "name");

ILookup<int, string> lookup = SingleItem.Lookup(42, new[] { "name", "other name" });

IReadOnlySet<int> set = SingleItem.Set("name");