如何重命名返回元组项目?

时间:2019-02-01 11:33:18

标签: c# methods tuples console-application c#-7.0

我不熟悉C#新功能,称为元组。我在Tuple上写了一些代码。当通过方法返回元组时(元组包含多个值)。

style.min.css

在真实情况下,识别哪个项目包含哪个值会变得很复杂?

所以我想提供一些特定的名称,而不是item1,item2等。 怎么做 ?

谢谢。

代码:

string firstNumber = separateNumbers.Item1;
string secondNumber = separateNumbers.Item2;
string thirdNumber = separateNumbers.Item3;
string fourthNumber = separateNumbers.Item4;

2 个答案:

答案 0 :(得分:3)

C# 7.0 tuples feature使用ValueTuple,而不是旧的Tuple

使用该功能,您可以执行以下操作:

namespace CSharp_Tuples
{
    class Program
    {
        static void Main(string[] args)
        {
            string PhoneNumbers = "+92315 +92316 +92317 +92318";

            var numbers = UsingValueTuple(PhoneNumbers);

            Console.WriteLine("firstNumber : " + numbers.firstNumber);
            Console.WriteLine("secondNumber : " + numbers.secondNumer);
            Console.WriteLine("thirdNumber : " + numbers.thirdNumber);
            Console.WriteLine("fourthNumber : " + numbers.fourthNumber);
        }

        static (string firstNumber, string secondNumer, string thirdNumber, string fourthNumber) UsingValueTuple(string allNumbers)
        {
            string[] numbers = allNumbers.Split(' ');
            return (numbers[0], numbers[1], numbers[2], numbers[3]);
        }
    }
}

答案 1 :(得分:1)

我进一步研究了这个问题,并得出了结论。

结论:

关于问题:

  

所以我想提供一些特定的名称,而不是item1,item2等。   该怎么做?

  • 字符串不允许我们重命名返回值,相反,我们可以利用 的匿名类型即可。

代码:

namespace CSharp_Tuples
{
    class Program
    {
        static void Main(string[] args)
        {
            string PhoneNumbers = "+92315 +92316 +92317 +92318";

            //var separateNumbers = GetIndividualNumbers(PhoneNumbers);

            //string firstNumber = separateNumbers.Item1;
            //string secondNumber = separateNumbers.Item2;
            //string thirdNumber = separateNumbers.Item3;
            //string fourthNumber = separateNumbers.Item4;

            //Console.WriteLine(firstNumber);
            //Console.WriteLine(secondNumber);
            //Console.WriteLine(thirdNumber);
            //Console.WriteLine(fourthNumber);


            var numbers = Cast(usingAnonymouseObject(PhoneNumbers), new {
                                                                        firstNumber = "",
                                                                        secondNumer = "",
                                                                        thirdNumber = "",
                                                                        fourthNumber = ""
                                                                        });
            Console.WriteLine("firstNumber : " + numbers.firstNumber);
            Console.WriteLine("secondNumber : " + numbers.secondNumer);
            Console.WriteLine("thirdNumber : " + numbers.thirdNumber);
            Console.WriteLine("fourthNumber : " + numbers.fourthNumber);


        }
        //static Tuple<string, string, string, string> GetIndividualNumbers(string allNumbers)
        //{
        //    string[] numbers  = allNumbers.Split(' ');
        //    return Tuple.Create<string,string,string,string>(numbers[0],numbers[1],numbers[2],numbers[3]);
        //}

        static object usingAnonymouseObject(string allNumbers)
        {
            string[] numbers = allNumbers.Split(' ');
            return new { firstNumber = numbers[0], secondNumer = numbers[1], thirdNumber = numbers[2], fourthNumber = numbers[3] };
        }

        static T Cast<T>(object obj, T type)
        {
            return (T)obj;
        }
    }
}