Procedurals
I am going to show you how to procedurally implement the array methods listed here
Again, you should not reinvent the wheel. These methods are built-in C#, but if you are curious, let's see them! 😏
IndexOf
First up, we are going to see the IndexOf method 😆
Let's try to find the index of an item in an array:
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
var index = -1;
var item = "cherry";
for (int i = 0; i < fruits.Length; i++)
{
if (fruits[i] == item)
{
index = i;
break;
}
}
Console.WriteLine(index); // 1
break
Once you find the item you are looking for, you can STOP the execution to make the algorithm more efficient (there is no need to go to the end of the array).
Exists
Let's see how to implement Exists
We can check to see if an item exists in an array based on a condition:
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
var exists = false;
for (int i = 0; i < fruits.Length; i++)
{
if (fruits[i].Contains("l"))
{
exists = true;
break;
}
}
Console.WriteLine(exists); // True
Find
Can we find an item in an array? 😏
We can simply find an item in an array (this returns the first occurrence):
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
string item = null;
for (int i = 0; i < fruits.Length; i++)
{
if (fruits[i].Contains("r"))
{
item = fruits[i];
break;
}
}
Console.WriteLine(item); // cherry
FindLast
Let's go from the end to the beginning 😁
We can find the last item that meets a condition:
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
string item = null;
for (int i = fruits.Length - 1; i >= 0; i--)
{
if (fruits[i].Contains("l"))
{
item = fruits[i];
break;
}
}
Console.WriteLine(item); // plum
FindIndex
We might want to find teh index of an item, right? 😄
Let's find the index of an item based on a condition:
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
var index = -1;
for (int i = 0; i < fruits.Length; i++)
{
if (fruits[i].Contains("l"))
{
index = i;
break;
}
}
Console.WriteLine(index); // 0
FindAll
Let's get multiple items 😉
We can also find all the items in an array that respect a certain condition:
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
var result = new string[fruits.Length];
var j = 0;
for (int i = 0; i < fruits.Length; i++)
{
if (fruits[i].Contains("p"))
{
result[j] = fruits[i];
j++;
}
}
foreach (var item in result)
{
Console.WriteLine(item);
}
Reverse
We might need some reversing as well 😬
Let's reverse the whole array:
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
var result = new string[fruits.Length];
for (int i = fruits.Length - 1; i >= 0; i--)
{
result[fruits.Length - i - 1] = fruits[i];
}
foreach (var item in result)
{
Console.WriteLine(item);
}
Copy
And the last one 😆
Let's copy the items of one array to another:
var fruits = new[] { "apple", "cherry", "pineapple", "plum" };
var result = new string[fruits.Length];
for (int i = 0; i < fruits.Length; i++)
result[i] = fruits[i];
foreach (var fruit in result)
Console.WriteLine(fruit);
Up Next
I have 8
so far, but in this post, there were 10
. Well, sorting and searching are more complex and they deserve separate posts. 😓