Assign values of array to separate variables in one line

前端 未结 8 1501
感情败类
感情败类 2020-12-11 00:24

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          


        
相关标签:
8条回答
  • 2020-12-11 00:57

    You can use named tuples with C# 7 now.

    {
      (string part1, string part2) = Deconstruct(new string[]{"hey","now"});
    }
    
    public (string, string) Deconstruct(string[] parts)
    {
       return (parts[0], parts[1]);
    }
    
    0 讨论(0)
  • 2020-12-11 01:07

    You can do this in C#

    string str1 = "hey", str2 = "now";
    

    or you can be fancy like this

            int x, y;
            int[] arr = new int[] { x = 1, y = 2 };
    
    0 讨论(0)
提交回复
热议问题