Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is a proper way to omit a type when working with generics?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

try this, using this

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading