-
Notifications
You must be signed in to change notification settings - Fork 0
Array Extensions
Description
Maps to Array.Sort
Array can be of any type.
Parameters
None
Usage
int[] arr1 = { 5, 1, 2, 4, 3 };
arr1.SortExt();OUTPUT: {1, 2, 3, 4, 5 }
Description
Maps to Maps to Array.BinarySearch
Uses binary search algorithm to search for value.
Parameters
value (object)
Usage
int[] arr1 = { 2, 5, 3, 4, 1 };`
int i = arr1.BinarySearchExt(3);Description
Maps to Maps to Array.BinarySearch
Uses binary search algorithm to search for value.
Parameters
index (int)
length (int)
value (object)
Usage
int[] arr1 = { 2, 5, 3, 4, 1 };
int i = arr1.BinarySearchExt(0, 3, 2);Description
Returns a decimal representing the average of a decimal array.
Parameters
None
Usage
decimal[] arr = { 1.0M, 2.0M, 3.0M, 4.0M, 5.0M, 6.0M };
decimal avg = arr.AverageExt();Description
Returns a double representing the average of an integer array.
Parameters
None
Usage
int[] arr = { 2, 5, 3, 4, 1 };
double avg = arr.AverageExt();Description
Returns a double representing the average of a double array.
Parameters
None
Usage
double[] arr = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
double avg = arr.AverageExt();Description
Maps to Array.Clear
Set a range of elements in an array to the default value of each element type.
Parameters
index (int)
length (int)
Usage
double[] arr = { 1, 2, 3, 4, 5 };
arr.ClearExt();Description
Performs a deep copy of an array containing reference types.
O(n) time complexity.
Parameters
index (int) - Optional
length (int) - Optional
Usage
string[] arr1 = { "one", "two", "three", "four", "five" };
string[] arr2 = arr1.CopyDeepExt();Description
Copies an array of type T into a new array of type T and inserts a value into the position specified.
This performs a deep copy of the first of array and of the value being inserted.
This extension method may have time complexities that make it costly to run on a large array.
Parameters
value (T)
position (int)
Usage
string[] arr1 = { "one", "two", "four", "five" };
string valToInsert = "three";
int position = 2;
string[] arr2 = arr1.InsertDeepExt(valToInsert, position);Description
Copies an array of type T into a new array of type T and inserts a value into the position specified.
This performs a shallow copy, so the generated array items will share the same memory location as the items in the first array and as the inserted value, if those values are reference types.
This extension method may have time complexities that make it costly to run on a large array.
Parameters
value (T)
position (int)
Usage
string[] arr1 = { "one", "two", "four", "five" };
string valToInsert = "three";
int position = 2;
string[] arr2 = arr1.InsertExt(valToInsert, position);Description
Concatenate two arrays. Shallow copy only.
For deep copy (ref types) use ConcatenateDeepExt.
Parameters
arr2 (T[])
Usage
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6 };
int[] newArr = arr1.ConcatenateExt(arr2);Description
Concatenate two arrays. Deep copy. For shallow copy use ConcatenateExt.
Type must implement ISerializable.
Not recommended for large arrays.
Parameters
arr2 (T[])
Usage
string[] arr1 = { "one", "two", "three" };
string[] arr2 = { "four", "five" };
string[] newArr = arr1.ConcatenateDeepExt(arr2);Description
Returns a new array with the same size as source array
Parameters
None
Usage
string[] arr1 = { "one", "two", "three", "four", "five" };
string[] arr2 = arr1.NewNullArrayExt();Description
Maps to string.Join
Joins the values in an IConvertible array into a string separated by the separator parameter.
Parameters
separator (string)
Usage
string[] arr1 = { "one", "two", "three", "four", "five" };
string str1 = arr1.JoinExt(" ");Description
Returns a double representing the median of an integer, double or decimal array.
Parameters
None
Usage
int[] arr = { -6, 2, -3, 14, 5, 26 };
double median = arr.MedianExt();Description
Returns the mode or modes of an int array as a generic list of integers.
Parameters
None
Usage
int[] arr1 = { -1, 1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 7, 9, 11 };
List<int> modes = arr1.ModeExt();Description
Returns the mode or modes of a double array as a generic list of doubles.
Parameters
None
Usage
double[] arr1 = { -1, 1, 1, 2, 3, 3, 3, 4, 5.8, 6, 6, 7, 9.5, 11 };
List<double> modes = arr1.ModeExt();Description
Returns the mode or modes of a decimal array as a generic list of decimals.
Parameters
None
Usage
decimal[] arr1 = { -1M, 1M, 1M, 2M, 3M, 3M, 3M, 4M, 5M, 6M, 6M, 7M, 9M, 11M };
List<decimal> modes = arr1.ModeExt();Description
Maps to Array.Exists
Takes an arrow function as a parameter.
Parameters
match (Predicate)
Usage
string[] arr1 = { "yellow", "green", "blue", "orange", "pink", "purple", "brown", "black", "white", "red", "mauve", "polka dot" };
bool exists = arr1.ExistsExt(x => x == "purple");Description
Checks if a string array contains a specific string.
Is case sensitive.
Uses Linq which may affect time complexity.
Parameters
match (string)
Usage
string[] arr1 = { "yellow", "green", "blue", "orange", "pink", "purple", "brown", "black", "white", "red", "mauve", "polka dot" };
bool contains = arr1.ContainsExt("purple");Description
Checks if an element in a string array starts with one or more letters (case sensitive).
Is case sensitive.
Uses Linq which may affect time complexity.
Parameters
match (string)
Usage
string[] arr1 = { "yellow", "green", "blue", "orange", "pink", "purple", "brown", "black", "white", "red", "mauve", "polka dot" };
bool startsWith = arr1.StartsWithExt("ora");Will returns true since orange starts with ora.
Description
Checks if an element in a string array ends with a one or more letters (case sensitive).
Uses Linq which may affect time complexity.
Parameters
match (string)
Usage
string[] arr1 = { "yellow", "green", "blue", "orange", "pink", "purple", "brown", "black", "white", "red", "mauve", "polka dot" };
bool endsWith = arr1.EndsWithExt("nge"));Will returns true since orange ends with ngw.
Description
Maps to Array.Find
Returns the first occurrence of an element in an array that matches the arrow function parameter.
Takes an arrow function as a parameter.
Uses Linq which may affect time complexity.
Parameters
value (Predicate)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10 };
int match = arr1.FindExt(x => x > 100);Description
Maps to Array.FindAll
Returns an array of all the matching elements in an array that match the arrow function parameter.
Takes an arrow function as a parameter.
Uses Linq which may affect time complexity.
Parameters
match (Predicate)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int[] matches = arr1.FindAllExt(x => x > 30 && x <= 40);Description
Maps to Array.FindIndex
Returns the zero-based index of the first element that matches the arrow function parameter.
Takes an arrow function as a parameter.
Uses Linq which may affect time complexity.
Parameters
match (Predicate)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int match = arr1.FindIndexExt(x => x == 45);Description
Maps to Array.FindLast
Returns the last occurrence of an element in an array that matches the arrow function parameter.
Takes an arrow function as a parameter.
Uses Linq which may affect time complexity.
Parameters
match (Predicate)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int match = arr1.FindLastExt(x => x == 45);Description
Maps to Array.FindLastIndex
Returns the zero-based index of the last element that matches the arrow function parameter.
Takes an arrow function as a parameter.
Uses Linq which may affect time complexity.
Parameters
match (Predicate)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int match = arr1.FindLastIndexExt(x => x == 45);Description
Returns an array of all the values that are between an lower bound and an upper bound, not inclusive.
Uses Linq which may affect time complexity.
Array can be int, double or decimal.
Parameters
lower (int)
upper (int)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int[] arr2 = arr1.FindBetweenExt(30, 40);Description
Returns an array of all the values that are between an lower bound and an upper bound, inclusive.
Uses Linq which may affect time complexity.
Array can be int, double or decimal.
Parameters
lower (int)
upper (int)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int[] arr2 = arr1.FindBetweenInclusiveExt(30, 40);Description
Returns an array of all the values that are greater than a target number, not inclusive.
Uses Linq which may affect time complexity.
Array can be int, double or decimal.
Parameters
target (int)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int[] arr2 = arr1.FindGreaterThanExt(30);Description
Returns an array of all the values that are greater than a target number, inclusive.
Uses Linq which may affect time complexity.
Array can be int, double or decimal.
Parameters
target (int)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int[] arr2 = arr1.FindGreaterThanInclusiveExt(30);Description
Returns an array of all the values that are less than a target number, not inclusive.
Uses Linq which may affect time complexity.
Array can be int, double or decimal.
Parameters
target (int)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int[] arr2 = arr1.FindLessThanExt(30);Description
Returns an array of all the values that are less than a target number, inclusive.
Uses Linq which may affect time complexity.
Array can be int, double or decimal.
Parameters
target (int)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
int[] arr2 = arr1.FindLessThanInclusiveExt(30);Description
Maps to Array.ForEach
Iterates through an array and performs the specified action.
Parameters
action (Action)
Usage
int[] arr1 = { 100, 45, 57, 85, 203, 125, 30, 10, 45, 155, 35, 45 };
Action<int> action = new Action<int>((val) => Console.WriteLine("{0:d} squared = {1:d}", val, val * val));
arr1.ForEachExt(action);Description
Maps to Array.Reverse
Reverses the order on the array.
Parameters
None
Usage
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
arr.ReverseExt(action);arr will equal { 7, 6, 5, 4, 3, 2, 1 }.
Description
Returns a string representation of the values in an array separated by a comma.
Optional string parameter to set separator, otherwise a comma is used.
Parameters
separator (string) - Optional
Usage
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
string arrString = arr.ToStringExt();arrString will equal "1,2,3,4,5,6,7".
Description
Returns an ArraySegment from an Array.
Parameters
offset (int)
length (int)
Usage
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
ArraySegment<int> arrSegment = arr.ToArraySegmentExt(1, 3);Description
Returns a splice of an array into a new array.
Parameters
offset (int)
length (int)
Usage
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
int[] arr2 = arr.SpliceExt(1, 3);Description
Maps to Array.Resize
Changes the size of the array to the value passed in the newSize parameter.
Because of Type constraints in .NET must pass in the array as a parameter by reference.
Parameters
sameArray (ref T[])
newSize (int)
Usage
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
arr.ResizeExt(ref arr, 8);Description
Returns all the duplicate values in an array in a new array.
Uses Linq which may affect time complexity.
Parameters
None
Usage
string[] arr = { "apples", "oranges", "bananas", "peaches", "apples", "pears", "bananas", "kiwis" };
string[] arrOfDups = arr.DuplicatesExt()arrOfDups will equal { "apples", "bananas" }.
Description
Returns a random value from an array.
If the array is null or empty, will throw an ArgumentNullException.
Parameters
None
Usage
string[] arr = { "apples", "oranges", "bananas", "peaches", "apples", "pears", "bananas", "kiwis" };
string randomFruit = arr.RandomExt();Description
Returns a string as Xml from the extended array, were each item of the array is an element in the xml string.
First paramater is a required element name.
Root name is optional. If not supplied to the method, the root element will be Root.
Namespace is optional.
Parameters
elementName (string)
root (string) Default value is "root"
ns (string) Default value is ""
Usage
string[] arr = { "apples", "oranges", "bananas", "peaches", "apples", "pears", "bananas", "kiwis" };
string root = "Colors";
string elementName = "Color";
string nameSpace = "https://www.namespace.com";
string xmlString = arr.ToXmlExt(elementName, root, nameSpace);Description
Returns an XDocument from the extended array, were each item of the array is an element in the XDocument.
First paramater is a required element name.
Root name is optional. If not supplied to the method, the root element will be Root.
Namespace is optional.
Parameters
elementName (string)
root (string) Default value is "root"
ns (string) Default value is ""
Usage
string[] arr = { "apples", "oranges", "bananas", "peaches", "apples", "pears", "bananas", "kiwis" };
string root = "Colors";
string elementName = "Color";
string nameSpace = "https://www.namespace.com";
XDocument xDocument = arr.ToXDocumentExt(elementName, root, nameSpace);Description
Converts the extended array to a Json array string.
If the array is not string types, will write the ToString result of each item.
Parameters
None
Usage
string[] arr = { "apples", "oranges", "bananas", "peaches", "apples", "pears", "bananas", "kiwis" };
string json = arr.ToJsonExt();Description
Converts the extended array to a Json string with a property that holds an array.
If the array is not string types, will write the ToString result of each item.
Parameters
propertyName (string)
Usage
string[] arr = { "apples", "oranges", "bananas", "peaches", "apples", "pears", "bananas", "kiwis" };
string propertyName = "colors";
string json = arr.ToJsonExt(propertyName);returns: { "colors":["apples","oranges","bananas","peaches","apples","pears","bananas","kiwis"] }
Description
Returns true if the extended array is null or empty.
Parameters
None
Usage
string[] arr1 = { "one", "two", "three" };
bool isNull = arr1.IsNullOrEmptyExt()); // false
string[] arr2 = null;
if (arr2.IsNullOrEmptyExt()) // true
{
// Do something...
}Description
Uses System.Xml.Serialization.XmlSerializer to serialize an array and return an xml string.
Parameters
None
Usage
string[] arr1 = { "one", "two", "four", "five" };
string xmlString = arr1.ToXmlSerializeExt();Output:
<?xml version="1.0" encoding="utf-16"?><ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><string>one</string><string>two</string><string>four</string><string>five</string></ArrayOfString>Description
Returns every nth value of an array in a new IEnumberable collection.
Parameters
occurence (string)
Usage
string[] arr1 = { "one", "two", "four", "five" };
string[] newArr = arr1.EveryExt(2).ToArray().Output:
{"two", "four"}
