0

I am trying to return the sum of the elements of a 2 dimensional array from my DLL, but can't seem to get it to work correctly. The returning integer which should be the 'sum' of the array elements returns a null. Could use some advice...

                int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

                Class1.arraySum(array2D);
                Console.WriteLine("Sum of 2D Array of numbers ({ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 })\n");
                Console.WriteLine(sum);

DLL

public static int arraySum(int[,] values)
    {
        int sum = values.Cast<int>().Sum();
        return sum;
    }

1 Answer 1

1

you have to declare and fill the sum variable

        int sum = Class1.arraySum(array2D);
        Console.WriteLine("Sum of 2D Array of numbers ({ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 })\n");
        Console.WriteLine(sum);

or write it down "on the fly":

        Console.WriteLine("Sum of 2D Array of numbers ({ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 })\n");
        Console.WriteLine(Class1.arraySum(array2D));
Sign up to request clarification or add additional context in comments.

3 Comments

So that doesn't send it to my DLL however. I want to have my DLL perform the sum method and then return the value
I missed the Class1 reference I took off for my test, see edited answer.
BTW you could reduce your arraySum() method body to one line return values.Cast<int>().Sum();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.