设为首页 加入收藏

TOP

C#函数返回值。
2019-09-17 18:24:19 】 浏览:16
Tags:函数 返回

一、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 ;
        }
    }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【手记】解决“未能创建 SSL/TLS .. 下一篇C#比较两个对象是否为同一个对象。

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目