C# method, method overloading, parameter modifier C# learning miscellaneous (7)

1. Define method and call

The definition method generally includes: modifier, return type, method name, parameter, method body.
The following example defines a method with no parameters and no return value, and a method with parameters and return value, and how to call in the main function For the convenience of demonstration, the modifier static is added before the method, which means that this method is a static method and can be called directly. If it is not a static method, it needs to be called with a class object.

        static void Main(string[] args)//程序入口,主函数
        {
    
    
            //无参无返回值的方法,直接调用即可
            outprint();

            //调用有参数的方法,需传入实参
            //如需得到返回值,一般需要一个变量来接受
            int result = add(5, 6);
        }

        static void outprint()//修饰符 返回类型 方法名
        {
    
    
            Console.WriteLine("hello !");//方法体
        }

        static int add(int a, int b)//修饰符 返回类型 方法名 形参
        {
    
    
            int s = a + b;
            return s;//返回值
        }

2. Method overload

In essence, there are multiple methods, but the method name is the same, and the number of parameters or parameter types of the method are different, forming an overload relationship. When calling, only different parameters need to be passed in, and then the overload can be used to execute different method bodies.

		static void Main(string[] args)
        {
    
    
            //根据传入的实参不同,执行不同的方法体
            methodCW("龙傲天");
            methodCW(55);
        }
        static void methodCW(string str)
        {
    
    
            Console.WriteLine("这是一个字符串:" + str);
        }
        static void methodCW(int a)
        {
    
    
            Console.WriteLine("这是一个整形数字:" + a);
        }

The program execution output results are as follows. Insert picture description here
From the output results, it can be seen that different method bodies are executed.

3. Parameter modifier

(1) ref reference parameter is
used when the actual parameter itself needs to be operated

        //ref 是引用变量地址,而普通的参数 int a 只是复制传入的实参,无法直接对变量本身进行更改
        static void ExChange(ref int a, ref int b)
        {
    
    
            int temp = a;
            a = b;
            b = temp;
        }

(2) The out output parameter is
mainly used to obtain multiple return values, which must be assigned before the method returns. It
solves the problem that C# methods can only have one return value

        static void Main(string[] args)
        {
    
    
            int s;
            int c = methodaa(5, 6, out s);//调用时,s 无需赋值
            Console.WriteLine(c+","+s);
        }
        //out 修饰符 也是引用变量地址
        //并且out 参数,在方法返回前必须赋值
        //这样就解决了C#方法只能有一个返回值的问题
        static int methodaa(int a, int b, out int s)
        {
    
    
            s = a + b;
            return a - b;
        }

(3) The params parameter array
can be realized. Any number of parameters can be passed in, and there is no limit on the number of parameters

        static void Main(string[] args)
        {
    
    
            float sum = GetSum(2, 1.2f, 3.1f);
        }
        static float GetSum(params float[] array)
        {
    
    
            float sum = 0;
            for (int i = 0; i < array.Length; i++)
            {
    
    
                sum += array[i];
            }
            return sum;
        }

Guess you like

Origin blog.csdn.net/qq_40385747/article/details/109048821