Day 11 of 30-Day .NET Challenge: Helper Methods — Array
Tags: csharp, programming, beginners, webdev
Published: 2024-03-30 16:03:52


## Introduction

The article demonstrates the use of various C# helper methods like Sort, Reverse, Clear and Resize.

### Learning Objectives

* Learn how to use helper methods like Sort() and Reverse()

* Learn how to use helper methods like Clear() and Resize()

### Prerequisites for Developers

* Familiar with arrays.

* Familiar with if statement

* Experience in running C# code through Visual Studio or Visual Studio Code.

## Getting Started

### How to use a Sort helper method

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.
```csharp
    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
```csharp
    #region Day 11 - Helper Methods  -  Array
    
    ArrayHelperMethods.SortExample();
    
    #endregion
```
### Console Output
```
    Before Sorting...
    B14,A11,B12,A13
    After Sorting...
    A11,A13,B12,B14
```
### How to use the Reverse helper method

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
```csharp
    /// <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
```csharp
    #region Day 11 - Helper Methods  -  Array
    
    ArrayHelperMethods.ReverseSortExample();
    
    #endregion
```
### Console Output
```
    Before Sorting...
    B14,A11,B12,A13
    After Reverse Sorting...
    A13,B12,A11,B14
```
### How to use the Clear Helper method

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
```csharp
    /// <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
```
### Console Output
```
    Clearing 2 ... count: 4
    ,,B12,A13
```
### How to use the Resize helper method

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.
```csharp
    /// <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
```csharp
    #region Day 11 - Helper Methods  -  Array
    
    ArrayHelperMethods.ResizeAndAdd();
    
    #endregion
```
### Console Output
```
    B14,A11,B12,A13
    Resizing 6 ... count: 6
    B14,A11,B12,A13,C01,C02
```
## Complete Code on GitHub
[GitHub — ssukhpinder/30DayChallenge.Net](https://github.com/ssukhpinder/30DayChallenge.Net/tree/main)

### C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

#### If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: [X](https://twitter.com/sukhsukhpinder) | [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh/) | [Dev.to](https://dev.to/ssukhpinder) | [Hashnode](https://dotnet.hashnode.dev/) | [Newsletter](https://www.linkedin.com/newsletters/net-programming-7031098498754195456/) | [Tumblr](https://www.tumblr.com/settings/blog/codewithsukh)

Visit our other platforms: [GitHub](https://github.com/ssukhpinder) | [Instagram](https://www.instagram.com/codewithsukh/) | [Tiktok](https://www.tiktok.com/@codewithsukh) | [Quora](https://www.quora.com/profile/Sukhpinder-Singh-4) | [Daily.dev](https://app.daily.dev/devcard)

More content at [C# Programming](https://medium.com/c-sharp-progarmming)