[Solved] Not All Code Paths Return A Value (C#)


Modify your addNumbers to return a value. Function signature states it returns int, so you must return int from the function.

using System;

public class Test
{
    public static int addNumbers(int num1, int num2) 
   {
    int result;
    result = num1 + num2;
    return result;
    }

    public static void Main()
    {
        int a = 2;
        int b = 2;
        int r;


        r = addNumbers(a, b);

        Console.WriteLine(r);
    }
}

EDIT:

You need addNumbers numbers = new addNumbers(); only if your function is not static.

Static functions can be called with ClassName.FunctonName, while non/static (instance functions) need to be called in a way you described.

addNumbers numbers = new addNumbers();
numbers.SomeFunction();

You can look it following way.

Classname.SomeStaticVariable = 2;

Way described above, SomeStaticVariable would be same for the entire application at any point. While way described below would be available only while obj exists in the memory.

Classname obj = new ClassName();
obj.SomeVariable = 2;

4

solved Not All Code Paths Return A Value (C#)