Khi tôi phải viết phương pháp mà trở lại hai giá trị, tôi thường đi về nó như trong đoạn mã sau đó trả về một List<string>
. Hoặc nếu tôi phải quay lại, ví dụ: số id và chuỗi, sau đó tôi trả lại List<object>
rồi chọn chúng ra với số chỉ số và recast giá trị.Cách tốt nhất để trả lại hai giá trị từ một phương thức là gì?
Việc tính lại và tham chiếu theo chỉ mục này có vẻ không phù hợp vì vậy tôi muốn phát triển một thói quen mới cho các phương thức trả về hai giá trị. Mô hình tốt nhất cho điều này là gì?
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();
}
}
}
}
Added:
Ok, nhờ mọi người, tôi thích "trả về một lớp tùy chỉnh" câu trả lời tốt nhất, không bao giờ thực sự nghĩ out
là dễ đọc, có vẻ như một hack để tôi trả lại biến đầu tiên theo một cách và biến thứ hai khác, đây là việc tái cấu trúc của tôi trở về lớp tùy chỉnh:
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; }
}
}
@tehMick Không thực sự chắc chắn lý do bạn tự mình lấy thẻ để xóa các thẻ đó? Họ dường như có liên quan đến tôi. –
Đừng quên giải pháp tốt nhất: tái cấu trúc mã để mọi phương thức đều trả về một cách hợp lý một điều. Thực tế là bạn có một phương thức trả về hai kết quả là một lá cờ đỏ chỉ ra rằng phương thức này có thể hưởng lợi từ việc tái cấu trúc. –