Reputation: 1
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
Upvotes: 0
Views: 331
Reputation: 50672
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.
Upvotes: 2
Reputation: 5993
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;
}
}
}
Upvotes: 4
Reputation: 3106
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..
Upvotes: 1
Reputation: 51711
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;
}
Upvotes: 2
Reputation:
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#.
Upvotes: 1
Reputation: 60694
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.
Upvotes: 6