DEV Community

Cover image for Day 11 of 30-Day .NET Challenge: Helper Methods — Array
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

Day 11 of 30-Day .NET Challenge: Helper Methods — Array

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.

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

Execute the code from the main method as follows

    #region Day 11 - Helper Methods  -  Array

    ArrayHelperMethods.SortExample();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

    Before Sorting...
    B14,A11,B12,A13
    After Sorting...
    A11,A13,B12,B14
Enter fullscreen mode Exit fullscreen mode

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

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

Execute the code from the main method as follows

    #region Day 11 - Helper Methods  -  Array

    ArrayHelperMethods.ReverseSortExample();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

    Before Sorting...
    B14,A11,B12,A13
    After Reverse Sorting...
    A13,B12,A11,B14
Enter fullscreen mode Exit fullscreen mode

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

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

Console Output

    Clearing 2 ... count: 4
    ,,B12,A13
Enter fullscreen mode Exit fullscreen mode

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.

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

Execute the code from the main method as follows

    #region Day 11 - Helper Methods  -  Array

    ArrayHelperMethods.ResizeAndAdd();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

    B14,A11,B12,A13
    Resizing 6 ... count: 6
    B14,A11,B12,A13,C01,C02
Enter fullscreen mode Exit fullscreen mode

Complete Code on GitHub

GitHub — ssukhpinder/30DayChallenge.Net

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 | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr

Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev

More content at C# Programming

Top comments (0)