Home > Back-end >  What is a proper way to omit a type when working with generics?
What is a proper way to omit a type when working with generics?

Time:11-06

I have HelperFunctions class which contains a lot methods for my assignment. One of those functions is simply printing the contents of an array:

    internal static void printArray(T[] arr)
    {
        foreach (var item in arr)
        {
            System.Console.WriteLine(item);
        }
    }

The issue here is that printArray is the only function inside the class HelperFunctions which requires type arguments. And as such, any other function call, needs a random type.

example:

System.Console.WriteLine("\n"   "Result of challenge 1: "   HelperFunctions<int>.printB64Res(ch1.hexToB64()));
byte[] input1 = HelperFunctions<int>.ProperHex(this.text1);
byte[] input2 = HelperFunctions<int>.ProperHex(this.text2);
HelperFunctions<string>.printArray(HelperFunctions<int>.readFile(dir   @"\hashes.txt")); 
// printArray() is the only function which needs a type argument

What would be a better alternative here?

CodePudding user response:

try this, using this

internal static void printArray<T>(T[] arr)
    {
        foreach (var item in arr)
        {
            System.Console.WriteLine(item);
        }
    }

  • Related