将数组的值分配给一行中的单独变量
我可以将数组中的每个值分配给 C# 中一行中的单独变量吗?这是我想要的 Ruby 代码示例:
irb(main):001:0> str1, str2 = ["hey", "now"]
=> ["hey", "now"]
irb(main):002:0> str1
=> "hey"
irb(main):003:0> str2
=> "now"
我不确定我想要的东西在 C# 中是否可行。
编辑:对于那些建议我将字符串“hey”和“now”分配给变量的人,这不是我想要的。想象一下以下情况:
irb(main):004:0> val1, val2 = get_two_values()
=> ["hey", "now"]
irb(main):005:0> val1
=> "hey"
irb(main):006:0> val2
=> "now"
现在,方法 get_two_values
返回字符串“hey”和“now”的事实是任意的。事实上它可以返回任意两个值,它们甚至不必是字符串。
Can I assign each value in an array to separate variables in one line in C#? Here's an example in Ruby code of what I want:
irb(main):001:0> str1, str2 = ["hey", "now"]
=> ["hey", "now"]
irb(main):002:0> str1
=> "hey"
irb(main):003:0> str2
=> "now"
I'm not sure if what I'm wanting is possible in C#.
Edit: for those suggesting I just assign the strings "hey" and "now" to variables, that's not what I want. Imagine the following:
irb(main):004:0> val1, val2 = get_two_values()
=> ["hey", "now"]
irb(main):005:0> val1
=> "hey"
irb(main):006:0> val2
=> "now"
Now the fact that the method get_two_values
returned strings "hey" and "now" is arbitrary. In fact it could return any two values, they don't even have to be strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这在 C# 中是不可能的。
我能想到的最接近的事情是在索引的同一行中使用初始化
This is not possible in C#.
The closest thing I can think of is to use initialization in the same line with indexs
更新:在 C#7 中,您可以使用元组轻松地一次分配多个变量。为了将数组元素分配给变量,您需要编写适当的
Deconstruct()
扩展方法:旧答案:
事实上,您可以在 C# 中实现类似的功能通过使用这样的扩展方法(注意:我没有包括检查参数是否有效):
并且您可以像这样使用它们:
如果需要函数的返回值,则以下重载将起作用:
这样:
您可以避免像 JaredPar 和其他人提出的解决方案中那样重复数组名称; “变量”列表很容易阅读。
您可以避免像 Daniel Earwicker 的解决方案中那样显式声明变量类型。
缺点是您最终会得到额外的代码块,但我认为这是值得的。您可以使用代码片段以避免手动输入大括号等。
我知道这是一个 7 年前的问题,但不久前我需要这样的解决方案 - 轻松为传递到方法中的数组元素命名(不,使用类/结构而不是数组是不切实际的,因为对于相同的数组我可能需要在不同的方法中使用不同的元素名称),不幸的是我最终得到了这样的代码:
现在我可以编写(事实上,我现在已经重构了其中一个方法!):
我的解决方案类似于模式在 F# 中匹配,我受到这个答案的启发:https://stackoverflow.com/a/2321922/6659843
Update: In C#7 you can easily assign multiple variables at once using tuples. In order to assign array elements to variables, you'd need to write an appropriate
Deconstruct()
extension methods:Old answer:
In fact, you can achieve similar functionality in C# by using extension methods like this (note: I haven't include checking if arguments are valid):
And you can use them like this:
In case a return value from a function is needed, the following overload would work:
In this way:
You avoid having to repeat array name like in solution proposed by JaredPar and others; the list of "variables" is easy to read.
You avoid having to explicitly declare variables types like in Daniel Earwicker's solution.
The disadvantage is that you end up with additional code block, but I think it's worth it. You can use code snippets in order to avoid typing braces etc. manually.
I know it's a 7 years old question, but not so long time ago I needed such a solution - easy giving names to array elements passed into the method (no, using classes/structs instead of arrays wasn't practical, because for same arrays I could need different element names in different methods) and unfortunately I ended up with code like this:
Now I could write (in fact, I've refactored one of those methods right now!):
My solution is similar to pattern matching in F# and I was inspired by this answer: https://stackoverflow.com/a/2321922/6659843
现实世界的用例是提供一种从函数返回多个值的便捷方法。因此,它是一个在数组中返回固定数量值的 Ruby 函数,调用者希望它们位于两个单独的变量中。这是该功能最有意义的地方:
要在 C# 中表达这一点,您可以在方法定义中用
out
标记两个参数,并返回void
:因此调用它:
不太好。希望 C# 的未来版本将允许这样做:
要启用此功能,
GetInfo
方法将返回Tuple
。这将是对语言的非破坏性更改,因为var
当前的合法使用非常严格,因此上述“多重声明”语法还没有有效的使用。The real-world use case for this is providing a convenient way to return multiple values from a function. So it is a Ruby function that returns a fixed number of values in the array, and the caller wants them in two separate variables. This is where the feature makes most sense:
To express this in C# you would mark the two parameters with
out
in the method definition, and returnvoid
:And so to call it:
It's not so nice. Hopefully some future version of C# will allow this:
To enable this, the
GetInfo
method would return aTuple<string, string>
. This would be a non-breaking change to the language as the current legal uses ofvar
are very restrictive so there is no valid use yet for the above "multiple declaration" syntax.您可以在一行中完成它,但不能作为一个语句来完成。
例如:
Python 和 ruby 支持您尝试执行的作业; C# 没有。
You can do it in one line, but not as one statement.
For example:
Python and ruby support the assignment you're trying to do; C# does not.
您可以将命名元组与 C# 7。
You can use named tuples with C# 7 now.
它不是。
It's not.
不,但是您可以初始化一个字符串数组:
尽管这对您来说可能不太有用。坦率地说,将它们分成两行并不难:
No, but you can initialize an array of strings:
Although that's probably not too useful for you. Frankly its not hard to put them on two lines: