Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Task - Implementing a Generic Method | Generics & Reflection
Advanced C# with .NET

Task - Implementing a Generic MethodTask - Implementing a Generic Method

You can clone the base code from the GithHub Repository.

We have a program which contains a method called ReverseArray. The method reversed an integer array.

In the main method, we are attempting to reverse a string array as well, however, it results in an error because the ReverseArray method does not support a string array.

Your task is to convert the ReverseArray method into a generic method so that the program is able to reverse and display both example and strExample arrays without any errors.

Nothing needs to be modified in the main method.

Check: If the program compiles and runs successfully, reversing and outputting both the int and string type arrays using the same reverseArray method, then your solution is correct.

Hint 1: The syntax for converting a method to a generic is methodName<T>(args, …);;
Hint 2: Convert the return type of the ReverseArray method into an array of type T (T being the generic type parameter);
Hint 3: Convert the argument and the variable type to an array of T;

namespace ConsoleApp
{
    internal class Program
    {
        public static T[] ReverseArray<T>(T[] array)
        {
            T[] reversedArray = new T[array.Length];
            int lastIndex = array.Length - 1;
            for (int i = 0; i < array.Length; i++)
                reversedArray[i] = array[lastIndex - i];
            return reversedArray;
        }

        static void Main(string[] args)
        {
            int[] example = new int[] { 1, 2, 3, 4, 5 };

            Console.WriteLine("Original Array:");
            foreach(var i in example)
                Console.Write(i + " ");

            Console.WriteLine("");

            example = ReverseArray(example);
            Console.WriteLine("Reversed Array:");
            foreach (var i in example)
                Console.Write(i + " ");

            Console.WriteLine("");

            string[] strExample = new string[] { "a", "b", "c", "d", "e" };
            Console.WriteLine("Original Array:");
            foreach (var i in strExample)
                Console.Write(i + " ");

            Console.WriteLine("");

            strExample = ReverseArray(strExample);
            Console.WriteLine("Reversed Array:");
            foreach (var i in strExample)
                Console.Write(i + " ");
        }
    }
}
      

Все було зрозуміло?

Секція 4. Розділ 3
course content

Зміст курсу

Advanced C# with .NET

Task - Implementing a Generic MethodTask - Implementing a Generic Method

You can clone the base code from the GithHub Repository.

We have a program which contains a method called ReverseArray. The method reversed an integer array.

In the main method, we are attempting to reverse a string array as well, however, it results in an error because the ReverseArray method does not support a string array.

Your task is to convert the ReverseArray method into a generic method so that the program is able to reverse and display both example and strExample arrays without any errors.

Nothing needs to be modified in the main method.

Check: If the program compiles and runs successfully, reversing and outputting both the int and string type arrays using the same reverseArray method, then your solution is correct.

Hint 1: The syntax for converting a method to a generic is methodName<T>(args, …);;
Hint 2: Convert the return type of the ReverseArray method into an array of type T (T being the generic type parameter);
Hint 3: Convert the argument and the variable type to an array of T;

namespace ConsoleApp
{
    internal class Program
    {
        public static T[] ReverseArray<T>(T[] array)
        {
            T[] reversedArray = new T[array.Length];
            int lastIndex = array.Length - 1;
            for (int i = 0; i < array.Length; i++)
                reversedArray[i] = array[lastIndex - i];
            return reversedArray;
        }

        static void Main(string[] args)
        {
            int[] example = new int[] { 1, 2, 3, 4, 5 };

            Console.WriteLine("Original Array:");
            foreach(var i in example)
                Console.Write(i + " ");

            Console.WriteLine("");

            example = ReverseArray(example);
            Console.WriteLine("Reversed Array:");
            foreach (var i in example)
                Console.Write(i + " ");

            Console.WriteLine("");

            string[] strExample = new string[] { "a", "b", "c", "d", "e" };
            Console.WriteLine("Original Array:");
            foreach (var i in strExample)
                Console.Write(i + " ");

            Console.WriteLine("");

            strExample = ReverseArray(strExample);
            Console.WriteLine("Reversed Array:");
            foreach (var i in strExample)
                Console.Write(i + " ");
        }
    }
}
      

Все було зрозуміло?

Секція 4. Розділ 3
some-alt