C#基础(8)——高级参数out、ref、params

1、out

一个方法返回不同类型的值,如int、string、char等,需要out。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] n = new int[11];
            int re_max;
            int re_min;
            double re_sum;
            double re_avg;
            Random r = new Random();
            for (int i = 0; i < n.Length; i++)
            {
                n[i] = r.Next(0, 12);
                Console.Write("{0}  ", n[i]);
            }
            Test(n, out re_max, out re_min, out re_sum, out re_avg);
            //真正将re_avg保留两位
            Console.WriteLine("\n返回的最大值:{0},最小值:{1},总和:{2},均值:{3:0.000}", re_max, re_min, re_sum, re_avg);
            re_avg = Convert.ToDouble(re_avg.ToString("0.00"));
            Console.WriteLine("真正re_avg保留两位:{0}", re_avg);
            Console.ReadKey();
        }

        /// <summary>
        /// 计算最大、最小、总和、均值
        /// </summary>
        /// <param name="nums"></param>
        /// <param name="max"></param>
        /// <param name="min"></param>
        /// <param name="sum"></param>
        /// <param name="avg"></param>
        public static void Test(int[] nums, out int max, out int min, out double sum, out double avg)
        {
            max = int.MinValue;
            min = int.MaxValue;
            sum = 0;
            avg = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
                if (nums[i] < min)
                {
                    min = nums[i];
                }
                sum += nums[i];
                avg = sum / nums.Length;
            }
        }
    }
}

这里写图片描述

使用多余out,进行用户的登录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

    class Program
    {
        static void Main(string[] args)
        {
            string msg;//不需要赋值,因为函数有返回值
            bool islogin = false;
            do
            {
                Console.WriteLine("用户名:");
                string name = Console.ReadLine();
                Console.WriteLine("密码:");
                string passwd = Console.ReadLine();
                islogin = Is_Login(name, passwd, out msg);
                Console.WriteLine(msg);
            } while (!islogin);
            Console.ReadKey();
        }

        public static bool Is_Login(string logname, string pwd, out string msg)
        {
            if (logname == "admin" && pwd == "123")
            {
                msg = "登录成功,欢迎回来!";
                return true;
            }
            else if (logname != "admin")//只能选中其中一种情况
            {
                msg = "登录名错误!";
                return false;
            }
            else if (pwd != "123")
            {
                msg = "密码错误!";
                return false;
            }
            else
            {
                msg = "未知错误...";
                return false;
            }
        }
    }
}

这里写图片描述

out参数

int.TryParse的模仿:
这里写图片描述
这里写图片描述

2、ref

将实参代入方法中进行修改,返回修改后了的值,不需要return一个定义的形参;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

    class Program
    {
        static void Main(string[] args)
        {
            double salary;
            Console.WriteLine("请输入当前薪水:");
            salary  = Convert.ToDouble(Console.ReadLine());
            TestRef(ref salary);//没有声明类型接收
            Console.WriteLine("加薪后:{0}", salary);
            Console.ReadKey();
        }

        public static void TestRef(ref double s)//没有返回类型
        {
            s += 500;
        }
    }
}

这里写图片描述

交换两个数字

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 10;
            int num2 = 20;
            TestRef(ref num1, ref num2);
            Console.WriteLine("交换后:num1={0}  num2={1}",num1,num2);
            Console.ReadKey();
        }

        /// <summary>
        /// 用于不另外开辟变量的情况下,交换两个变量值
        /// </summary>
        /// <param name="n1">第一个变量</param>
        /// <param name="n2">第二个变量</param>
        public static void TestRef(ref int n1,ref int n2)//没有返回类型
        {
            n1 = n1 - n2;
            n2 = n1 + n2;
            n1 = n2 - n1;
        }
    }
}

这里写图片描述

判断两个数字的大小:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

    class Program
    {
        public static int i = 0;
        static void Main(string[] args)
        {
            Console.WriteLine("第一个数字:");
            int n1 = GetInt(Console.ReadLine()); 
            Console.WriteLine("第二个数字:");
            int n2 = GetInt(Console.ReadLine());
            JudgeNumber(ref n1, ref n2);

            Console.WriteLine("最后的两个数:{0},{1}", n1, n2);
            Console.ReadKey();
        }

        public static int GetInt(string s)
        {
            while (true)
            {
                try
                {

                    int number = Convert.ToInt32(s);
                    return number;
                }
                catch
                {
                    Console.WriteLine("输入有误!请重新输入:");
                    s = Console.ReadLine();
                }
            }
        }
        public static void JudgeNumber(ref int n1, ref int n2)
        {
            while (true)
            {
                if (n1 < n2)
                {
                    return;
                }
                else
                {
                    Console.WriteLine("再次第一个数字:");
                    n1 = GetInt(Console.ReadLine());
                    Console.WriteLine("再次第二个数字:");
                    n2 = GetInt(Console.ReadLine());
                }
            }
        }

    }
}

这里写图片描述

params

params可变参数,且参数为最后的定义,用于减少声明的参数,如改进前:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 4, 5 };
            Tets("张三", array);
            Console.ReadKey();
        }

        public static void Tets(string name, int[] grade)
        {
            int sum = 0;
            for (int i = 0; i < grade.Length; i++)
            {
                sum += grade[i];
            }
            Console.WriteLine("{0}的总成绩是:{1}", name, sum);
        }

    }
}

改进后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{

    class Program
    {
        static void Main(string[] args)
        {
            //int[] array = { 1, 2, 3, 4, 5 };
            Tets("张三", 1, 2, 3, 4, 5);
            Console.ReadKey();
        }

        public static void Tets(string name, params int[] grade)
        {
            int sum = 0;
            for (int i = 0; i < grade.Length; i++)
            {
                sum += grade[i];
            }
            Console.WriteLine("{0}的总成绩是:{1}", name, sum);
        }

    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值