求下面的代码的简写

caicoko 2008-07-09 08:58:12
Select 方法有四个重载方法,都是string类型的,如下
Select(string s1)
Select(string s1, string s2)
Select(string s1, string s2, string s3)
Select(string s1, string s2, string s3, string s4)

我想增加下面一个方法,来根据参数的多少来决定重载上面的参数
Select(string[] args)

除了if和case一个一个判断还有其他简单方法吗?

...全文
111 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzlxy 2008-07-11
  • 打赏
  • 举报
回复
用string Select(params string[] str) 一个函数就可以了

经典的string.Format() 函数就是这种格式
Magic_YJL 2008-07-11
  • 打赏
  • 举报
回复
这样你只需要写一个方法,不用重载那么多方法了
Magic_YJL 2008-07-11
  • 打赏
  • 举报
回复
没必要这样做,因为系统会根据你方法的参数进行自动匹配的,没必要决定调用哪个方法。
如果你这个方法的参数不定的话,建议这样写:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace JustTry
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Select("1", "2"));
Console.WriteLine(Select("1", "2", "3"));
Console.WriteLine(Select("1", "2", "3", "4"));
Console.ReadLine();
}

public static string Select(params string[] str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
sb.Append(str[i]);
}
return sb.ToString();
}
}
}
一品梅 2008-07-11
  • 打赏
  • 举报
回复
好像有个parms参数决定参数多少的,忘了.
sheng9hhd 2008-07-11
  • 打赏
  • 举报
回复
除了if来判断.length,我也想知道还有没有什么好点的方法

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧