欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#一个方法返回多个值示例

程序员文章站 2023-12-20 20:11:16
复制代码 代码如下:static void main(string[] args)        {&...

复制代码 代码如下:

static void main(string[] args)
        {
            //声明
            int value;
            string stroutvalue;

            //调用函数
            //函数的参数有两个返回的值
            returnmulvalue(out stroutvalue,out value);

            console.writeline("call returnmulvalue(out string strout, out int i)");
            console.writeline("value = {0:n}", value);//value = 1.00
            console.writeline("out string value = " + stroutvalue); //out string value = out function


            //调用函数 http://www.cnblogs.com/roucheng/
            //函数原返回值与参数中均得到返回的值
            value = returnmulvalue(out stroutvalue);

            console.writeline("call returnmulvalue(out string strout)");
            console.writeline("value = {0:n}", value);//value = 2.00
            console.writeline("out string value = " + stroutvalue);//out string value = out function 2

            //声明
            int[] outarr;
            //调用函数
            //函数返回数组
            returnmularr(out outarr, out value);

            console.writeline("call returnmularr(out int[] arr, out int i)");
            console.writeline("value = {0:n}", value);//value = 3.00
            console.writeline("out arr value ");

            for (int j = 0; j < outarr.length; j++)
            {
                console.write(outarr[j]);//12345
            }
        }

        /// <summary>
        /// 返回多个值,原函数返回空
        /// </summary>
        /// <param name="strout">使用out关键字返回的字符串</param>
        /// <param name="i">使用out关键字返回的数值</param>
        static void returnmulvalue(out string strout, out int i)
        {
            i = 1;
            strout = "out function";
        }

        /// <summary>
        /// 返回多个值,原函数返回数值
        /// </summary>
        /// <param name="strout">使用out关键字返回的字符串</param>
        /// <returns>原函数直接返回的数值</returns>
        static int returnmulvalue(out string strout)
        {
            int i = 2;
            strout = "out function 2";
            return i;
        }


        /// <summary>
        /// 返回数组
        /// </summary>
        /// <param name="arr">使用out关键字返回的数组</param>
        /// <param name="i">使用out关键字返回的数值</param>
        static void returnmularr(out int[] arr, out int i)
        {
            arr = new int[5] { 1, 2, 3, 4, 5};
            i = 3;
        }

上一篇:

下一篇: