Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
namespace CommandLine
{
    class Program
    {
        int a = 10, b = 10, c;
        public void add()
        {
            c = a + b;
            
        }
        static void Main(string[] args)
        {
            
            Console.WriteLine("additon:",c);
           
        }
    }
}

//shows error i am new to .Net please explain how to add two numbers and print sum
Posted
Updated 17-Sep-13 7:15am
v2
Comments
Richard C Bishop 17-Sep-13 13:10pm    
"I am new to dot net please explain how to add two numbers and print sum" is not an error message. More info.
[no name] 17-Sep-13 13:18pm    
You need to add the "static" keyword to your variable declarations and the add method declaration
static int a = 10, b = 10, c;
public static void add()
{
c = a + b;

}

You need to call the add method to get the result. You also need to learn how to format strings with parameters, and how to use parameters in methods. I would strongly suggest you get a book on C# and work through it to learn the basics.

.NET Book Zero[^] is a very good online primer.
 
Share this answer
 
First thing, "shows error i am new to .Net please explain" would not be a statement of a problem. A simple Google[^] search would provide you millions of result,so you probably don't try that.

Now the second thing, suggestions provided by ThePhantomUpvoter and abbaspirmoradi are quite good,you can follow that.I just see you another way to do it with just 2 lines of code that could add 2 integer numbers.See:
C#
class Program
{
    //int a = 10, b = 10, c;
    public static int add(int a, int b)
    {
        return a + b;
    }
    static void Main(string[] args)
    {
        Console.WriteLine("additon:" + add(10,10));
    }
}

So i changed your void add() method to static int so that it can return a value and just call it inside the Main to print the result.

Cheers!
Shuvro
 
Share this answer
 
I just correct your code.this was easy.but my friend if you new in major first try and try .this
is clear if you have got error at first.don't worry.many book exist try with them.

look at this are:

http://www.apress.com/9781430210337/[^]

http://freecomputerbooks.com/Programming-C-Sharp-for-Beginners.html[^]


C#
namespace CommandLine
{
    class Program
    {
     
 static int c = 0, a = 10, b = 10;

        public static int add(int a1, int b1)
        {
            return c = a1 + b1;
        }

        private static void Main(string[] args)
        {
            c = add(a, b);
            Console.WriteLine("additon:{0}", c);
            Console.ReadLine();

        }
    }
}
 
Share this answer
 
v9
Comments
[no name] 17-Sep-13 13:58pm    
I would hope the downvoter would care to comment or at least rethink the downvote.
abbaspirmoradi 17-Sep-13 14:10pm    
Yes, you right..
try
C#
Console.WriteLine("addition:" + c);
 
Share this answer
 
Comments
ridoy 17-Sep-13 13:28pm    
so you think this is right?! do you check inside your code before posting it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900