Edw*_*uay 10 c# methods return-value
当我必须编写返回两个值的方法时,我通常会按照以下代码返回一个List<string>.或者,如果我必须返回例如id和字符串,那么我返回a List<object>然后用索引号选择它们并重新设置值.
这种通过索引进行重铸和引用似乎不够优雅,所以我想为返回两个值的方法开发一种新的习惯.这个的最佳模式是什么?
using System;
using System.Collections.Generic;
using System.Linq;
namespace MultipleReturns
{
class Program
{
static void Main(string[] args)
{
string extension = "txt";
{
List<string> entries = GetIdCodeAndFileName("first.txt", extension);
Console.WriteLine("{0}, {1}", entries[0], entries[1]);
}
{
List<string> entries = GetIdCodeAndFileName("first", extension);
Console.WriteLine("{0}, {1}", entries[0], entries[1]);
}
Console.ReadLine();
}
/// <summary>
/// gets "first.txt", "txt" and returns "first", "first.txt"
/// gets "first", "txt" and returns "first", "first.txt"
/// it is assumed that extensions will always match
/// </summary>
/// <param name="line"></param>
public static List<string> GetIdCodeAndFileName(string line, string extension)
{
if (line.Contains("."))
{
List<string> parts = line.BreakIntoParts(".");
List<string> returnItems = new List<string>();
returnItems.Add(parts[0]);
returnItems.Add(line);
return returnItems;
}
else
{
List<string> returnItems = new List<string>();
returnItems.Add(line);
returnItems.Add(line + "." + extension);
return returnItems;
}
}
}
public static class StringHelpers
{
public static List<string> BreakIntoParts(this string line, string separator)
{
if (String.IsNullOrEmpty(line))
return null;
else
{
return line.Split(new string[] { separator }, StringSplitOptions.None).Select(p => p.Trim()).ToList();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
好的,谢谢大家,我喜欢" 返回一个自定义类 "的答案最好,从来没有真正想过out那么容易阅读,看起来像是一个黑客对我一路返回第一个变量而另一个另一个,这里是我的重构返回一个自定义课程:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MultipleReturns
{
class Program
{
static void Main(string[] args)
{
string extension = "txt";
{
IdCodeFileNamePair pair = GetIdCodeAndFileName("first.txt", extension);
Console.WriteLine("{0}, {1}", pair.IdCode, pair.FileName);
}
{
IdCodeFileNamePair pair = GetIdCodeAndFileName("first", extension);
Console.WriteLine("{0}, {1}", pair.IdCode, pair.FileName);
}
Console.ReadLine();
}
/// <summary>
/// gets "first.txt", "txt" and returns "first", "first.txt"
/// gets "first", "txt" and returns "first", "first.txt"
/// it is assumed that extensions will always match
/// </summary>
/// <param name="line"></param>
public static IdCodeFileNamePair GetIdCodeAndFileName(string line, string extension)
{
if (line.Contains("."))
{
List<string> parts = line.BreakIntoParts(".");
List<string> returnItems = new List<string>();
return new IdCodeFileNamePair { IdCode = parts[0], FileName = line };
}
else
{
List<string> returnItems = new List<string>();
return new IdCodeFileNamePair { IdCode = line, FileName = line + "." + extension };
}
}
}
public static class StringHelpers
{
public static List<string> BreakIntoParts(this string line, string separator)
{
if (String.IsNullOrEmpty(line))
return null;
else
{
return line.Split(new string[] { separator }, StringSplitOptions.None).Select(p => p.Trim()).ToList();
}
}
}
public class IdCodeFileNamePair
{
public string IdCode { get; set; }
public string FileName { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ath 12
我更喜欢创建一个具有两个属性的轻量级类(见下文),或者使用一个元组(现在可以在.NET 4中加入框架但不难编写自己的元组)
class MyReturnValue
{
public string Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)