Skip to main content

Command Palette

Search for a command to run...

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

Published
3 min read
Day 11 of 30-Day .NET Challenge: Helper Methods — Array
S

Hi there 👋 I'm Sukhpinder Singh, a passionate self-taught .Net developer from India. Worked on 10+ projects for SQL database design. Developed and maintained over 20 .NET websites with 100% client satisfaction.

Skills C# | Microsoft Certified | Medium Blogger | .Net Core | Xamarin | ASP.Net | Angular


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));
    }
}

Execute the code from the main method as follows

#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

/// <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

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

/// <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.

/// <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

Console Output

B14,A11,B12,A13
Resizing 6 ... count: 6
B14,A11,B12,A13,C01,C02

Complete Code on GitHub

GitHub — ssukhpinder/30DayChallenge.Net
Contribute to ssukhpinder/30DayChallenge.Net development by creating an account on GitHub.github.com


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

30 Day C# Challenge

Part 20 of 30

The series consists of a 30-day code challenge where I'll be diving deep into a different C# concept every single day. 🌟 I'll also be sharing my learnings and insights through engaging articles.

Up next

Day 10 of 30-Day .NET Challenge: File Paths

The article demonstrates the built-in functions while working with file system paths. It makes it easier to handle file paths.