zoukankan      html  css  js  c++  java
  • C#函数返回值。

    一、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 ;
            }
        }
  • 相关阅读:
    easyui datetimebox 日期控件绑定双击日期选择时间
    js 中call和apply的应用
    js中数组的合并和对象的合并
    flex也可以让背景透明
    收集了一些as的面试题,给HR准备的
    [转]PureMVC的十个小提示
    12个Flex常用功能代码
    43个热门Flex和ActionScript 3.0 APIs,技巧和工具[转]
    转载+原创PureMVC 实例讲解
    PureMVC使用时的注意事项
  • 原文地址:https://www.cnblogs.com/zhangyuhao/p/10488609.html
Copyright © 2011-2022 走看看