C#入门8.2——方法的声明及调用(2)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//引用命名空间
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication4
{
    class Program
    {
        //编写一个方法用于计算给定整型数组元素的和
        //用到了params(多参数的意思),用于不指定的多参数函数
        static int Add(params int[] nums)
        {
            int sum = 0;
            foreach (int outnum in nums) sum += outnum;
            return sum;
        }
        static void Main(string[] args)
        {
            int[] myintArray = { 2, 3, 4, 5, 6 };
            Console.WriteLine(Add(myintArray));

            Console.ReadKey();
        }

    }
}


引用参数与值参数

1.值参数:传值给方法,方法中的变量修改不影响参数列表

2.引用参数:ref关键字指定

使用ref两个限定

1)调用函数变量必须非常量

2)调用变量必须初始化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//引用命名空间
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication4
{
    class Program
    {
        //编写一个方法用于求平方(没有返回值)
        //此时的参数传递叫做值传递
        static void Square(int num)
        {
            num *= num;
            Console.WriteLine("num的平方是"+num);
        }
        static void Main(string[] args)
        {
            int num = 10;
            Console.WriteLine(num);//num值为10
            Square(num);//函数中num值变为100
            Console.WriteLine(num);//主函数中num值仍然为10
            Console.ReadKey();
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//引用命名空间
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication4
{
    class Program
    {
        //编写一个方法用于求平方(没有返回值)
        //此时的参数传递叫做值传递
        static void Square(ref int num)//在方法这里加了一个ref关键字
        {
            num *= num;
            Console.WriteLine("num的平方是"+num);
        }
        static void Main(string[] args)
        {
            int num = 10;
            Console.WriteLine(num);
            Square(ref num);//引用的时候也加ref关键字
            Console.WriteLine(num); //主函数中的num变成了100,这里就是引用传递。
            Console.ReadKey();
        }

    }
}


输出参数

执行方式与ref完全一样,关键字为out

当希望返回多个值时,out非常有用

输出参数与引用参数的区别

未初始化的变量用作ref非法,out合法

函数调用out参数量,必须把它当作尚未赋值(既可以把已赋值的变量当作out参数,但存储在该变量中的值在方法执行时会丢失)

out ref必须在调用及定义方法时声明

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//引用命名空间
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication4
{
    class Program
    {
        //编写一个方法,求出数组中最大的值进行返回,并返回最大值的一个索引
        static int maxNum(int[] nums, out int maxNumIndex)
        {
            int maxNum = nums[0];
            maxNumIndex = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (maxNum < nums[i])
                {
                    maxNum = nums[i];
                    maxNumIndex = i;
                }
            }
            return maxNum;
        }
    static void Main(string[] args)
    {
            int[] myintArray = { 1, 2, 5, 4, 9 };
            int maxNumIndex;
            Console.WriteLine("此数组的最大值是{0},最大值的索引是{1}",maxNum(myintArray,out maxNumIndex),maxNumIndex);
        Console.ReadKey();
    }

}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值