DEV Community

Matthew Cullen
Matthew Cullen

Posted on

1

C# Useful Array.Methods()

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay