gpt4 book ai didi

c# - 从方法返回两个值的最佳方法是什么?

转载 作者:可可西里 更新时间:2023-11-01 08:54:13 25 4
gpt4 key购买 nike

当我必须编写返回两个值的方法时,我通常按照下面的代码来处理它,它返回一个 List<string>。 .或者如果我必须返回,例如id 和 string,然后我返回一个 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();
}
}
}
}

添加:

好的,谢谢大家,我最喜欢“返回自定义类”的答案,没想到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; }
}
}

最佳答案

我更喜欢创建一个具有两个属性的轻量级类(见下文),或者使用一个元组(现在可以在 .NET 4 中烘焙到框架中,但不难编写你自己的)

class MyReturnValue
{
public string Id { get; set; }
public string Name { get; set; }
}

关于c# - 从方法返回两个值的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2774015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com