0
namespace csfunction
{
    class Program
    {
        static void Main(string[] args)
        {
            public int AddNumbers(int number1, int number2)
            {
                int result = AddNumbers(10, 5);
                Console.WriteLine(result);    
            }
        }
    } 
}

is my code...am getting error like.....Error Type or namespace definition, or end-of-file expected
..hw can i over come this error

3
  • 2
    That's like an infinite loop of recursion. I don't understand what you are trying to do. Commented Apr 12, 2011 at 6:44
  • No, We are going to do homework for swathi. Commented Apr 12, 2011 at 6:50
  • 2
    @Yan Sklyarenko, because it is extra fun to have a main method on a web server that causes a SO exception and has the admin looking for the console output everywhere ;-) Commented Apr 12, 2011 at 11:48

6 Answers 6

6

You are defining a method inside another method, and that is not allowed in C#.

Change it to this:

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

        public static int AddNumbers(int number1, int number2)
        {
           int result = AddNumbers(10, 5);
           Console.WriteLine(result);
        }
    } 
}

Then if you want to call AddNumbers from Main, add the following line inside Main

AddNumbers( 10, 5);

Also note that you are not using the parameters inside AddNumbers for anything. The method should probably look like this:

public int AddNumbers(int number1, int number2)
{
  int result = number1 + number2;
  Console.WriteLine(result);
}

In it's current form it's calling itself recursively also, and will go into an endless loop.

So basically, there are tons of problems with your code. You should probably try to get an entry level book on C# to brush up on your C# skills.

Sign up to request clarification or add additional context in comments.

Comments

4

The function AddNumbers should be outside the Main method

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

        int result = AddNumbers(10, 5);
        Console.WriteLine(result);
    }


      static int AddNumbers(int number1, int number2)
      {
          return number1 + number2;
      }
  }   
}

2 Comments

Furthermore, AddNumbers will recurse into a StackOverflowException.
sure will, didn't even look at the content of the method. edited.
2

Methods don't work like that in C#. The middle bit needs to look like this

static void Main(string[] args)
{
    int result = AddNumbers(10, 5);
    Console.WriteLine(result);
}
public static int AddNumbers(int number1, int number2)
{
    return number1 + number2;
}

Comments

2

Try this:

namespace csfunction
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The answer: " + AddNumbers(40, 2));
        }

        public static int AddNumbers(int number1, int number2)
        {
            int result = number1 + number2;
            return result;
        }
    } 
}

You nested the functions instead of making them functions of the same class.

Comments

1
static void Main(string[] args)
        {
            public int AddNumbers(int number1, int number2)
            {
                int result = AddNumbers(10, 5);
                Console.WriteLine(result);    
            }
        }

You're defining your method AddNumbers inside another method Main. That is not possible in C#.

Comments

1

Do like that:

   namespace csfunction
    {
        class Program
        {
            static void Main(string[] args)
            {
              AddNumbers(8,5);
            }
            public int AddNumbers(int number1, int number2)
            {
              int result = AddNumbers(10, 5);
              Console.WriteLine(result);    
            }
        } 
    }

Dont try inside function..

1 Comment

This won't compile, make AddNumbers static

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.