简体   繁体   English

如何从一个方法分别返回两个字符串而无需追加?

[英]how can return two string from a method separately without appending?

I have method that will receive a string input(with message & mode name) and the method will separate the string input in to two strings(1.message 2.mode name) according to separator. 我有一个将接收字符串输入(带有消息和模式名称)的方法,并且该方法将根据分隔符将字符串输入分成两个字符串(1.message 2.mode name)。 but i need to return this separated two string at a time.please provide me a good way to do this.can i use "out" and return in one method? 但是我需要一次返回这个分开的两个字符串。请为我提供一个很好的方法。我可以使用“ out”并返回一个方法吗? if yes, please tell me any links to do the out parameter. 如果是,请告诉我任何执行out参数的链接。 (or) any good way if you have. (或)您有任何好的方法。

You can use out parameters: 您可以使用out参数:

string myMethod(string input, out secondOutput)
{
   secondOutput="bla";
   return "xyz";
}

you can also use 2 out parameters: 您还可以使用2个out参数:

void myMethod(string input, out firstOutput, out secondOutput)
{
   firstOutput="bla";
   secondOutput = "xyz";
}

Or like others suggested and I think is preferrable most of the time, using an array: 或者像其他建议的那样,我认为在大多数情况下最好使用数组:

string[] myMethod(string input)
{
   return new string[] {firstOutput, secondOutput);
}

If you can tell us what the method is for we might help you choosing the best alternative. 如果您能告诉我们该方法的用途,我们可能会帮助您选择最佳的替代方法。

为什么不将2个字符串放入Array并返回该Array?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM