This repository was archived by the owner on Nov 13, 2018. It is now read-only.
Repository navigation
Methods
Jordan Kniest edited this page Jun 17, 2017
·
13 revisions
Here you can find explanations and examples for all collection methods.
The reduce method converts a collection into some other type. For example it could be used to sum the items (converting the collection to a single integer)
Structure:
public T Reduce<T>(ReduceCallback<T> callable, T initial);Example:
// Create a new collection with three integer values (10, 20, 30)
var collection = new Collection<int>(10, 20, 30);
// Reduce this collection to a single integer value
var result = collection.Reduce((value, item) => {
return value + item;
}, 0);
// Print the result to the console
Console.Write(result); // The result is: 60Return all items as an array. It is in fact an alias for the 'ToArray' method.
Structure:
public TList[] All();Example:
// Create a new collection with three integer values (10, 20, 30)
var collection = new Collection<int>(10, 20, 30);
// Request all items
var result = collection.All();
// Print the first item to the console
Console.Write(result[0]); // The result is: 10Return the average value based on the return value of the callable.
Structure:
public int Avg(IntItemCallback callable);Example:
// Create a new collection with three persons (the first parameters is their age)
var collection = new Collection<int>(new Person(10), new Person(45), new Person(36));
// Calculate the average age
var result = collection.Avg(person => person.Age);
// Print the result to the console
Console.Write(result); // The result is: 30Return the median value based on the return value of the callable.
Structure:
public int Median(IntItemCallback callable);Example:
// Create a new collection with three persons (the first parameters is their age)
var collection = new Collection<int>(new Person(10), new Person(45), new Person(36));
// Calculate the median age
var result = collection.Median(person => person.Age);
// Print the result to the console
Console.Write(result); // The result is: 45Return the mode value(s) of the collection.
Structure:
public Collection<TList> Mode();Example:
// Create a new collection with six integers
var collection = new Collection<int>(1, 1, 4, 1, 3, 3);
// Calculate the mode value
var result = collection.Mode();
// Print the result to the console
Console.Write(result); // The result is: 1