C# Creating and using Functions

后端 未结 11 714
暗喜
暗喜 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:10

    This code gives you an error because your Add function needs to be static:

    static public int Add(int x, int y)
    

    In C# there is a distinction between functions that operate on instances (non-static) and functions that do not operate on instances (static). Instance functions can call other instance functions and static functions because they have an implicit reference to the instance. In contrast, static functions can call only static functions, or else they must explicitly provide an instance on which to call a non-static function.

    Since public static void Main(string[] args) is static, all functions that it calls need to be static as well.

提交回复
热议问题