用 C# 中的函数返回两个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16504998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:11:08  来源:igfitidea点击:

Return two strings with a function in C#

c#stringreturn

提问by Kevin

I have a function where I want to return two values. Is this possible?

我有一个函数,我想返回两个值。这可能吗?

This is my code, but it doesn't seem to like that I want to return two values:

这是我的代码,但它似乎不喜欢我想返回两个值:

public string PlayerCards(string player1C1, string player1C2)
{
    generatedCard = randomCard.Next(1, 52);
    player1C1 = generatedCard.ToString();
    player1C1 = player1C1 + ".png";
    return player1C1, player1C2;
}

采纳答案by Jon Skeet

Some options:

一些选项:

  • Use an outparameter:

    public string PlayerCards(out string x)
    

    Return one value, and set the outparameter (xin this case) to another value; the calling code will need to specify an argument with outas well, and after the call has completed, the caller will be able to see the value set in the method.

    (It's not clear why you're accepting parameters at all; you don't seem to really use them.)

  • Return a ValueTuple<string, string>, ideally using C# 7 tuples to provide element names

  • Return a Tuple<string, string>
  • Create a new type to store the two values together, assuming it's a meaningful combination. This is definitely a good choice if the values are related in a way which you'll use elsewhere. For example, instead of having a method returning one string for the suit of a card and one for the value, you'd create a PlayingCardtype.
  • Refactor your code into two method calls, each of which return a single value
  • 使用out参数:

    public string PlayerCards(out string x)
    

    返回一个值,并将out参数(x在本例中)设置为另一个值;调用代码也需要指定一个参数,out调用完成后,调用者将能够看到方法中设置的值。

    (不清楚您为什么要接受参数;您似乎并没有真正使用它们。)

  • 返回 a ValueTuple<string, string>,理想情况下使用 C# 7 元组来提供元素名称

  • 返回一个 Tuple<string, string>
  • 创建一个新类型来将两个值存储在一起,假设它是一个有意义的组合。如果值以您将在其他地方使用的方式相关,这绝对是一个不错的选择。例如,不是让方法返回一个用于卡片花色的字符串和一个用于值的字符串,而是创建一个PlayingCard类型。
  • 将您的代码重构为两个方法调用,每个调用都返回一个值

It's not at all clear what your code is trying to do - the name of the method isn't clear and you don't use the parameters. When you've clarified what the method is trying to achieve - to yourself as much as to us - the answer may well become more obvious.

根本不清楚您的代码要做什么 - 方法的名称不明确并且您没有使用参数。当您澄清了该方法试图实现的目标时——对你自己和对我们——答案很可能会变得更加明显。

I'd also encourage you to use local variables where appropriate - I suspect generatedCardshould be a local variable instead of the (presumably) instance variable it currently is.

我还鼓励您在适当的情况下使用局部变量 - 我怀疑generatedCard应该是一个局部变量,而不是它目前的(大概)实例变量。

回答by fatihk

You can return tuple: Tuple<string, string>

您可以返回元组: Tuple<string, string>

Tuple<string, string> t = new Tuple<string, string>(player1C1,player1C2);

return t;

回答by Amit Mittal

One of the several possible options:

几种可能的选择之一:

Create a struct like this:

创建一个这样的结构:

struct Players
{
  public string Player1;
  public string Player2;
}

Then use it in your function like this:

然后在您的函数中使用它,如下所示:

public Players PlayerCards()
    {   
        Players p1;
        generatedCard = randomCard.Next(1, 52);
        p1.Player1 = generatedCard.ToString();
        p1.Player2 =  p1.Player1 + ".png";            
        return p1;
    }

回答by Mohsen Bahman

I think that you can use string array...

我认为您可以使用字符串数组...

Second way is to use a struct containing two string values or a class with two string member,,

第二种方法是使用包含两个字符串值的结构或具有两个字符串成员的类,

Look at here:

看这里:

    /// <summary>
    /// Using struct
    /// </summary>
    struct twoStringValue
    {
        public string s1, s2;
    }

    public twoStringValue PlayerCards(string player1C1, string player1C2)
    {
        twoStringValue tsv;
        generatedCard = randomCard.Next(1, 52);
        tsv.s1 = player1C1 = generatedCard.ToString();
        tsv.s1 = player1C1 = player1C1 + ".png";
        return tsv;
    }


    /// <summary>
    /// Using a class
    /// </summary>
    class TwoStringValue
    {
        public string str1;
        public string str2;
    }

    public TwoStringValue PlayerCards(string player1C1, string player1C2)
    {
        TwoStringValue tsv;
        generatedCard = randomCard.Next(1, 52);
        tsv.str1 = player1C1 = generatedCard.ToString();
        tsv.str1 = player1C1 = player1C1 + ".png";
        return tsv;
    }

Good Luck.

祝你好运。