c# 用out获取递归返回值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RecursiveDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "1;#张三;#2;#李四;#5;#王五;#10;#马六";//源字符串
            int counter = 1;//计数器
            string ids = "";//ids初始为空
            string result = "";//结果字符串
            GetIds(source, counter, ids, out result);//获取结果
            Console.WriteLine(result);
            Console.ReadLine();
        }
        /// <summary>
        /// 获取id
        /// </summary>
        /// <param name="source"></param>
        /// <param name="counter"></param>
        /// <param name="ids"></param>
        /// <param name="result"></param>
        private static void GetIds(string source, int counter,string ids,out string result)
        {
            int startIndex = 0;
            int endIndex = source.IndexOf(";#");
            int length = endIndex - startIndex;
            if (counter % 2 == 1)
            {
                string id = source.Substring(startIndex, length);
                ids += id + ";";
            }
            string newExport = source.Substring(endIndex + 2, source.Length - length - 2);
            if (newExport.Contains(";#"))
            {
                GetIds(newExport, counter + 1,ids,out result);
            }
            else
            {
                result = ids;
            }
        }
    }
}


你可能感兴趣的:(C#,out)