【C#关键字】out、ref、params

1、方法的简历

public static 返回值类型 方法名(参数列表)
{
     方法体;
}

public: 访问修饰符,公开的,公共的,哪都可以访问。
static: 静态的
返回值类型:不需要写返回值就写void
方法名:Pascal每个单词的首字母大写
参数列表: 完成这个方法所必须提供给这个方法的条件

2、out

public static bool GetNum(int num, out string str)
{
    if (num < 100)
    {
        str = "这个数字小于100";
        return false;
    }
    else
    {
        str = "这个数字大于于100";
        return true;
    }
}
static void Main(string[] args)
{
    while (true)
    {
        int a = Convert.ToInt32(Console.ReadLine());
        string str;
        bool b = GetNum(a, out str);
        Console.WriteLine(b.ToString() + str);
    }
}

3、ref

public static void GetNum(ref int num)
{
    num = num + 5;
}
static void Main(string[] args)
{
    while (true)
    {
        int a = Convert.ToInt32(Console.ReadLine());
        GetNum(ref a);
        Console.WriteLine(a);
    }
}

4、params

public static int GetNum(string name, params int[] score)//params参数必须是最后一个
{
    int sum = 0;
    for (int i = 0; i < score.Length; i++)
    {
        sum = sum + score[i];
    }
    return sum;
}
static void Main(string[] args)
{
    // int[] s = { 1, 2, 3, 4, 5 };
    int sum = GetNum("zhangsan", 1, 2, 3, 4, 5, 6, 7, 8, 9);
    Console.WriteLine(sum);
    Console.ReadLine();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值