C# Creating and using Functions

后端 未结 11 683
暗喜
暗喜 2020-12-28 18:08

I need help with C# programming; I am new to it and I come from C background. I have a Console Application like this:

using System;
using System.Collections         


        
11条回答
  •  一生所求
    2020-12-28 18:27

    you have to make you're function static like this

    namespace Add_Function
    {
      class Program
      {
        public static void(string[] args)
        {
           int a;
           int b;
           int c;
    
           Console.WriteLine("Enter value of 'a':");
           a = Convert.ToInt32(Console.ReadLine());
    
           Console.WriteLine("Enter value of 'b':");
           b = Convert.ToInt32(Console.ReadLine());
    
           //why can't I not use it this way?
           c = Add(a, b);
           Console.WriteLine("a + b = {0}", c);
        }
        public static int Add(int x, int y)
        {
            int result = x + y;
            return result;
         }
      }
    }
    

    you can do Program.Add instead of Add you also can make a new instance of Program by going like this

    Program programinstance = new Program();
    

提交回复
热议问题