2

How do I get the following program to print the result of sumOfNumbers method? I was unable to call this method in the main method and I'm not sure why. Can someone explain what I have done wrong?

class Program
{
    static void Main(string[] args)
    {


        //Console.WriteLine();
        //Console.ReadLine();
    }

    private int sumOfNumbers (int x, int y)
    {
        return x + y;
    }
}
jwheron
  • 2,553
  • 2
  • 30
  • 40
OO_Learner
  • 73
  • 1
  • 2
  • 8
  • Some of the solutions below remove the static Main method. While this addresses the question, `Program.Main` is usually the entry point for a console application, so this approach isn't useful. – neontapir Jul 25 '13 at 15:40

6 Answers6

6

Your method has the wrong signature. It should be private static int sumOfNumbers(int x, int y) .

edit: I've been asked to put a little more explanation around this. A static method, like Main in your example, doesn't belong to a particular instance of an object. It belongs to its class, Program. A method without the static modifier belongs to a particular instance of the class. This difference means that an "objectless" static method (Main) can only work directly with methods that are static, or by instantiating an object and then invoking that method on the object.

This second method would look like:

var foo = new Program();
Console.WriteLine(foo.sumOfNumbers(3,2))

I would continue to recommend modifying the method to be static. I infer from your Main method that you're writing a console app, and it would be unconventional (in my experience) to instance such a class.

Reacher Gilt
  • 1,813
  • 12
  • 26
  • at least explain why, http://stackoverflow.com/questions/3017708/why-cant-you-call-a-non-static-method-from-a-static-method – mihail Jul 25 '13 at 15:34
  • Just for clarification, the reason why the OP can't see the `sumOfNumbers` method is because you can't call an *instance* method from a `static` method. – James Jul 25 '13 at 15:35
  • Fantastic that's very clear thank you. I will read up more regarding method signatures. – OO_Learner Jul 25 '13 at 15:51
2

you either need to instantiate your Program and call sumOfNumbers, or you can make sumOfNumbers static.

var program = new Program();
var sum = program.sumOfNumbers(1, 2);
Console.WriteLine(sum);

Or, if you put the static keyword in front of your sumOfNumbers implementation, then you could call it with Program.sumOfNumbers(1, 2);

jltrem
  • 12,124
  • 4
  • 40
  • 50
1

Understanding about "static" keyword would help you. Refer What's a "static method" in C#?

The concept here is, one method is "static" method and other method is "instance" method.

You cannot call instance method from a static method without instanciating the class. However vice-versa should be possible.

Hence if you convert your method signature as,

private static int sumOfNumbers (int x, int y)

you can able to call this method from main(), a static method

Community
  • 1
  • 1
karpanai
  • 235
  • 4
  • 14
0

Summing up:

class Program
{
    static void Main(string[] args)
    {
        Console.Writeline(sumOfNumbers(5,5));
    }

    private static int sumOfNumbers (int x, int y)
    {
        return x + y;
    }
}
bavaza
  • 10,319
  • 10
  • 64
  • 103
0

write the following code in main

Program p=new Program();
Console.Write(p.sumOfNumbers(<any number>,<any number>);
Console.ReadKey();
  • While this is true, `Program` is usually the entry point for a console application, so this approach isn't useful. – neontapir Jul 25 '13 at 15:38
-2

Console.Writeline(sumOfNumbers(5,5).Tostring);

Wizengamot
  • 150
  • 1
  • 7
  • 1
    You don't need `ToString`, nor would this work because the method signature has not been marked as `static`. – James Jul 25 '13 at 15:34