The article demonstrates the use of various C# helper methods like Sort, Reverse, Clear and Resize.
Utilize the Array class’s Sort() method to arrange the elements in the array in alphanumeric order. To begin, create a static class file called “ArrayHelperMethods.cs” within the console application. Insert the provided code snippet into this file.
public static class ArrayHelperMethods
{
/// <summary>
/// Outputs
/// Before Sorting...
/// B14, A11, B12, A13
/// After Sorting...
/// A11, A13, B12, B14
/// </summary>
public static void SortExample()
{
Console.WriteLine("Before Sorting...");
string[] pallets = { "B14", "A11", "B12", "A13" };
Console.WriteLine(string.Join(",", pallets));
Array.Sort(pallets);
Console.WriteLine("After Sorting...");
Console.WriteLine(string.Join(",", pallets));
}
}
Execute the code from the main method as follows
#region Day 11 - Helper Methods - Array
ArrayHelperMethods.SortExample();
#endregion
Before Sorting...
B14,A11,B12,A13
After Sorting...
A11,A13,B12,B14
In the following example, let’s execute the Reverse() method from the Array class to invert the sequence of elements. To do that, add another method into the same static class as shown below.
/// <summary>
/// Outputs
/// Before Sorting...
/// B14,A11,B12,A13
/// After Reverse Sorting...
/// A13,B12,A11,B14
/// </summary>
public static void ReverseSortExample() {
Console.WriteLine("Before Sorting...");
string[] pallets = { "B14", "A11", "B12", "A13" };
Console.WriteLine(string.Join(",", pallets));
Array.Reverse(pallets);
Console.WriteLine("After Reverse Sorting...");
Console.WriteLine(string.Join(",", pallets));
}
Execute the code from the main method as follows
#region Day 11 - Helper Methods - Array
ArrayHelperMethods.ReverseSortExample();
#endregion
Before Sorting...
B14,A11,B12,A13
After Reverse Sorting...
A13,B12,A11,B14
The Array.Clear() method helps to clear the value of specified elements within the array. To do that, add another method into the same static class as shown below
/// <summary>
/// Outputs
/// Clearing 2 ... count: 4
/// ,,B12,A13
/// </summary>
public static void ClearExample()
{
string[] pallets = { "B14", "A11", "B12", "A13" };
Console.WriteLine("");
Array.Clear(pallets, 0, 2);
Console.WriteLine($"Clearing 2 ... count: {pallets.Length}");
Console.WriteLine(string.Join(",", pallets));
}
Execute the code from the main method as follows
#region Day 11 - Helper Methods - Array
ArrayHelperMethods.ClearExample();
#endregion
Clearing 2 ... count: 4
,,B12,A13
In the following example, let’s expand the array size from 4 to 6, then add two new numbers at index 4 and 5. The two newly added elements will remain null until the value is assigned.
/// <summary>
/// Outputs
/// B14,A11,B12,A13
/// Resizing 6 ... count: 6
/// B14,A11,B12,A13,C01,C02
/// </summary>
public static void ResizeAndAdd() {
string[] pallets = { "B14", "A11", "B12", "A13" };
Console.WriteLine(string.Join(",", pallets));
Array.Resize(ref pallets, 6);
Console.WriteLine($"Resizing 6 ... count: {pallets.Length}");
pallets[4] = "C01";
pallets[5] = "C02";
Console.WriteLine(string.Join(",", pallets));
}
Execute the code from the main method as follows
#region Day 11 - Helper Methods - Array
ArrayHelperMethods.ResizeAndAdd();
#endregion
B14,A11,B12,A13
Resizing 6 ... count: 6
B14,A11,B12,A13,C01,C02
GitHub — ssukhpinder/30DayChallenge.Net
Thank you for being a part of the C# community! Before you leave:
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr
Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev
More content at C# Programming
Also published here.