C# Creating and using Functions

后端 未结 11 639
暗喜
暗喜 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();
    
    0 讨论(0)
  • 2020-12-28 18:33
    static void Main(string[] args)
        {
            Console.WriteLine("geef een leeftijd");
            int a = Convert.ToInt32(Console.ReadLine());
    
            Console.WriteLine("geef een leeftijd");
            int b = Convert.ToInt32(Console.ReadLine());
    
            int einde = Sum(a, b);
            Console.WriteLine(einde);
        }
        static int Sum(int x, int y)
        {
            int result = x + y;
            return result;
        }
    
    0 讨论(0)
  • 2020-12-28 18:34

    You should either make your Add function static like so:

    static public int Add(int x, int y)
    { 
        int result = x + y;
        return result;
     } //END   Add
    

    static means that the function is not class instance dependent. So you can call it without needing to create a class instance of Program class.

    or you should create in instance of your Program class, and then call Add on this instance. Like so:

    Program prog = new Program();
    prog.Add(5,10);
    
    0 讨论(0)
  • 2020-12-28 18:34

    Because your function is an instance or non-static function you should create an object first.

    Program p=new Program();
    p.Add(1,1)
    
    0 讨论(0)
  • 2020-12-28 18:36

    Note: in C# the term "function" is often replaced by the term "method". For the sake of this question there is no difference, so I'll just use the term "function".

    Thats not true. you may read about (func type+ Lambda expressions),( anonymous function"using delegates type"),(action type +Lambda expressions ),(Predicate type+Lambda expressions). etc...etc... this will work.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(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());
    
               Func<int, int, int> funcAdd = (x, y) => x + y;
               c=funcAdd.Invoke(a, b);
               Console.WriteLine(Convert.ToString(c));
    
            }
    
         }
    }
    
    0 讨论(0)
提交回复
热议问题