Array.IndexOf()
- returns index of item inside array
- params: (array, value to find)
Example
var colors = new[] { "red", "green", "blue" };
var result = Array.IndexOf(colors, "red");
WriteLine(result); // 0
Array.Exists()
- returns a boolean if conditions are met
- params: (array, conditions)
Example
var colors = new[] { "red", "green", "blue" };
var result = Array.Exists(colors, color => color.Contains("green"));
WriteLine(result); // true
Array.Find()
- returns the first item to meet a condition
- params: (array, condition)
Example
var colors = new[] { "red", "green", "blue" };
var result = Array.Find(colors, color => color.Contains("green"));
WriteLine(result); // "green"
Array.FindLast()
- Starts search from the LAST index
- returns the first item to meet a condition
- params: (array, condition)
Array.FindIndex()
- returns the index of the element that meets the condition
- params: (array, condition)
Example
var colors = new[] { "red", "green", "blue" };
var result = Array.FindIndex(colors, color => color.Contains("green"));
WriteLine(result); // 1
Array.FindAll()
- returns array of all conditions met
- params: (array, condition)
Example
var colors = new[] { "red", "green", "blue" };
var result = Array.FindAll(colors, color => color.Contains("r"));
foreach (var color in result)
{
WriteLine(color);
}
// "red"
// "green"
Array.Reverse()
- returns array with all elements in reverse order
- params: (array)
Example
var colors = new[] { "red", "green", "blue" };
var result = Array.Reverse(colors);
foreach (var color in result)
{
WriteLine(color);
}
// "blue"
// "green"
// "red"
Array.Copy()
- copy elements to a new array
- can specify how many elements to copy
- params: (originalArray, copy, numberOfElementsToCopy)
Example
var colors = new[] { "red", "green", "blue" };
var colors2 = new string[colors.length];
Array.Copy(colors, colors2, 2);
foreach (var color in colors2)
{
WriteLine(color);
}
// "red"
// "green"
Array.Sort()
- sort the elements of an array
- params: (array)
Example
var colors = new[] { "red", "green", "blue" };
Array.Sort(colors);
foreach (var color in colors)
{
WriteLine(color);
}
// "blue"
// "green"
// "red"
Array.BinarySearch()
- search a sorted array
- returns the index of the found item
- returns -1 if nothing is found
Example
var colors = new[] { "red", "green", "blue" };
Array.Sort(colors); // "blue", "green", "red"
var index = Array.BinarySearch(colors, "green");
if (index == -1)
{
WriteLine("not found");
}
else
{
WriteLine(index); // 1
}
Top comments (0)