当前位置: 移动技术网 > IT编程>开发语言>.net > C#函数返回值。

C#函数返回值。

2019年03月08日  | 移动技术网IT编程  | 我要评论

青春h2之开心老人,王牌大明星刘德华,红旗颂背景音乐mp3

一、params.

  可变参数,无论有几个参数,必须出现在参数列表的最后,可以为可变参数直接传递一个对应类型的数组。

class program
    {
        static void main(string[] args)
        {
            test("msg");
            test("msg", 1, 2, 3);
            int[] intarry = new int[] { 1, 2, 3 };
            test("msg", intarry);

        }
        static void test(string msg,params int[] args)
        {

        }
    }

二、ref

  引用传递

三、out

  out 参数在使用之前必须在方法里为out参数赋值。

  out参数无法获取实参传来的值。所以在主函数 中,只需声明函数就行。它也是引用。

  out一般用在函数有多个返回值。

  参数前加ref   out 不能算重载。

 class program
    {
        static void main(string[] args)
        {
            test(out int x);
            console.writeline(x);
        }
       static void test(out int x)
        {
            x = 100;
        }
    }

out 实例:

class program
    {
        static void main(string[] args)
        {
            console.writeline("输入用户名");
            string id = console.readline();
            console.writeline("输入密码");
            string psw = console.readline();
            bool isok=login(id, psw, out string msg);
            if (isok)
            {
                console.writeline(msg);
            }
            else
            {
                console.writeline(msg);
            }

        }

        private static bool login(string id, string psw, out string msg)
        {
            bool isok = false;
            if (id!="admin")
            {
                msg = "用户名错误";
            }
            if (psw!="123")
            {
                msg = "密码错误";
            }
            else
            {
                isok = true;
                msg = "登录成功";
            }
            return isok ;
        }
    }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网