Declaration and call of method function in C#

Article Directory


First, the syntax method functions of
two, two methods function
calls three methods

One, the syntax of the method function

[Access modifier] static return value method name ([parameter list])
{ method body }

For example:

Public  static int Number(int num1,int num2)
{
    
    
	return num1+num2;
}

Two, two methods function

  1. No parameters and no return value

Such as:

Public static void show()
{
    
    
}

There are no parameters and return values ​​in this method function, void means empty, and the return value is empty.

  1. There are parameters and return values

Such as:

Public  static int Number (int num1,int num2)
{
    
    
return
}

The parameters of this method are int type num1, num2, and the return value is int type. Because it has a return value, there is a return statement in the method body.

Three, method call
There are two methods of call method,

1. Use class name plus method name
such as:Program.IsNUmber();

2. Use the method name directly

Such as:IsNumber();

Specific code examples:

using System;

namespace 方法有返回值有参数
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int sum =add(3,5);
            Console.WriteLine("两个数的和为{0}", sum);
            Console.ReadKey();
        }
        /// <summary>
        /// 此方法用来求两个数的和
        /// </summary>
        /// <param name="num1">第一个数</param>
        /// <param name="num2">第二个数</param>
        /// <returns></returns>
        public static int add(int num1, int num2)
        {
    
    
            int sum = 0;
            sum = num1 + num2;
            return sum;
        }
    }
}

Guess you like

Origin blog.csdn.net/wangwei021933/article/details/108757636