C# Creating and using Functions

后端 未结 11 687
暗喜
暗喜 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条回答
  •  梦毁少年i
    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);
    

提交回复
热议问题