zoukankan      html  css  js  c++  java
  • 13.out参数

    什么是out?

    在方法中想要返回多个值(尤其是多个不同类型的值)可以考虑使用out参数来解决。

    如何做?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Demo {
    
    
        class Program {
            static void Main(string[] args) {
                
                //对于out修饰的参数来说,方法的内部必须赋值,外部可以不赋值
                bool flag;
                int money;
                string message=Test("张三", 19, out flag, out money);
    
                Console.WriteLine(message);
                Console.WriteLine(flag);
                Console.WriteLine(money);
                Console.ReadKey();
               
            }
    
            public static string Test(string name, int age,out bool flag,out int money) {
    
                //对于out修饰的参数来说,方法的内部必须赋值,外部可以不赋值
                flag = true;
                money = 200;
                return name+age;
            }
            
        }
    }
    
    

    运行结果:

    如代码所示,对于想要返回多个值的方法来说,只需要使用out修饰,想要返回的形参就行了,如:

    public static string Test(string name, int age,out bool flag,out int money) {
    
          flag = true;
          money = 200;
          return name+age;
    }
    

    在其他地方调用时,在对应的实参上加上out修饰,如:

    string message=Test("张三", 19, out flag, out money);
    

    对于实参的名字可以与形参不同,这没有强制规定。如图,这样是完全没有问题的。

  • 相关阅读:
    python安装pip
    MySQL免安装版
    git仓库删除所有提交历史记录,成为一个干净的新仓库
    git地址带上密码,不用每次提交都输入密码
    virtualenv
    mac卸载python
    换源
    屏幕旋转,ViewController触发事件
    ViewController启动加载过程
    使用 symbolicatecrash转化crash日志
  • 原文地址:https://www.cnblogs.com/lz32158/p/12823972.html
Copyright © 2011-2022 走看看