C#,方法就能生成多个值 [英] C#, method that outputs multiple value

查看:138
本文介绍了C#,方法就能生成多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#。



我有多个输出值的方法。我知道的退出但这是一个引用类型。
有什么等同于价值型,但可以从一个方法还输出多个值。与引用类型,该值也改变了方法,所以我想,以防止外面。

 


解决方案

我知道的出的但是这是一个引用类型。




目前还不清楚你的意思。 退出不是一个类型在所有 - 它是参数的装饰。

  //讨厌,但它的工作... 
:你可以参考类型或值类型使用无效SplitInTwo(字符串输入,出串X1,出字符串X2,
OUT INT actualSplitCount)
{
的String []位= input.Split('/');
×1 =位[0];
×2 =比特[1];
actualSplitCount = bits.Length;
}



所以你的可以的使用退出无论哪种方式。不过,我会强烈建议你的的这样做。您可以使用元组家庭类型特设多个值,但如果返回值实际上是相关的,你应该考虑将它们封装到一个单独的类型,并返回一个值该类型。


I am using c#.

I have a method that outputs multiple values. I am aware of out but this is a reference type. Is there anything equivalent to value type but that can also output multiple values from a method. With the reference type, the values are also changed outside of the method so I like to prevent.

    out

解决方案

I am aware of out but this is a reference type.

It's not clear what you mean. out isn't a type at all - it's a decorator for parameters. You can use it with reference types or value types:

// Nasty, but it does work...
void SplitInTwo(string input, out string x1, out string x2, 
                out int actualSplitCount)
{
    string[] bits = input.Split('/');
    x1 = bits[0];
    x2 = bits[1];
    actualSplitCount = bits.Length;
}

So you can use out either way. However, I would strongly advise you not to do so. You can use the Tuple family of types for ad hoc multiple values, but if the returned values are actually related, you should consider encapsulating them into a separate type, and returning a value of that type.

这篇关于C#,方法就能生成多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆