首页 > 其他 > 详细

13.out参数

时间:2020-05-03 23:01:22      阅读:53      评论:0      收藏:0      [点我收藏+]

什么是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);

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

技术分享图片

13.out参数

原文:https://www.cnblogs.com/lz32158/p/12823972.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!