《零基础学C#》第七章-实例04:不同类型参数方法的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wtxhai/article/details/88793366
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example704
{
    class Program
    {
        private int Add(int x, int y)//值参数
        {
            x = x + y;//对x进行加y操作
            return x;//返回x
        }
        private int Add(ref int x, int y)//ref参数
        {
            x = x + y;//对x进行加y操作
            return x;//返回x
        }
        private int Add(int x, int y, out int z)//out参数
        {
            z = x + y;//记录x+y的结果
            return z;//返回out参数z
        }
        private int Add(params int[] x)//params参数
        {
            int result = 0;//记录运算结果
            for (int i = 0; i < x.Length; i++)//遍历参数数组
            {
                result += x[i];//执行相加操作
            }
            return result;//返回运算结果
        }
        static void Main(string[] args)
        {
            Program pro = new Program();//创建Program对象
            int x = 30;//定义实参变量x
            int y = 40;//定义实参变量y
            int z;
            Console.WriteLine("值参数的使用:" + pro.Add(x, y));
            Console.WriteLine("值参数中实参x的值:" + x);//输出值参数方法中实参x的值
            Console.WriteLine("ref参数的使用:" + pro.Add(ref x, y));
            Console.WriteLine("ref参数中实参x的值:" + x);//输出ref参数方法中实参x的值
            Console.WriteLine("out参数的使用:" + pro.Add(x, y, out z));
            Console.WriteLine("params参数的使用:" + pro.Add(20, 30, 40, 50, 60));
            Console.ReadLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wtxhai/article/details/88793366