您的位置:首页 > 编程语言 > C#

c# 用out获取递归返回值

2010-11-05 15:44 169 查看
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