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

我是C#新功能的新手,称为元组。我在元组上写了一些代码。当元组由方法返回时(元组包含多个值)。


断续器


string firstNumber = separateNumbers.Item1;

string secondNumber = separateNumbers.Item2;

string thirdNumber = separateNumbers.Item3;

string fourthNumber = separateNumbers.Item4;

在实际的单词场景中,识别哪个项目包含哪个值变得复杂?


所以我想提供一些具体的名称,而不是项目1,项目2等...如何做到这一点?


提前致谢。


法典:


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);


        }

        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]);

        }

    }

}


胡子哥哥
浏览 94回答 2
2回答

慕田峪9158850

C# 7.0 元组功能使用&nbsp;ValueTuple,而不是旧的元组。使用该功能,您可以执行如下操作:namespace CSharp_Tuples{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string PhoneNumbers = "+92315 +92316 +92317 +92318";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var numbers = UsingValueTuple(PhoneNumbers);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("firstNumber : " + numbers.firstNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("secondNumber : " + numbers.secondNumer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("thirdNumber : " + numbers.thirdNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("fourthNumber : " + numbers.fourthNumber);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static (string firstNumber, string secondNumer, string thirdNumber, string fourthNumber) UsingValueTuple(string allNumbers)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] numbers = allNumbers.Split(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (numbers[0], numbers[1], numbers[2], numbers[3]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

繁华开满天机

进一步我研究了这个问题,我得到了结论。结论:关于问题:所以我想提供一些具体的名称,而不是项目1,项目2等...如何做到这一点?元组不允许我们重命名返回值,相反,我们可以使用匿名类型。法典:namespace CSharp_Tuples{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string PhoneNumbers = "+92315 +92316 +92317 +92318";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var separateNumbers = GetIndividualNumbers(PhoneNumbers);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //string firstNumber = separateNumbers.Item1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //string secondNumber = separateNumbers.Item2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //string thirdNumber = separateNumbers.Item3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //string fourthNumber = separateNumbers.Item4;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Console.WriteLine(firstNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Console.WriteLine(secondNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Console.WriteLine(thirdNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Console.WriteLine(fourthNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var numbers = Cast(usingAnonymouseObject(PhoneNumbers), new {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstNumber = "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secondNumer = "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thirdNumber = "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fourthNumber = ""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("firstNumber : " + numbers.firstNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("secondNumber : " + numbers.secondNumer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("thirdNumber : " + numbers.thirdNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("fourthNumber : " + numbers.fourthNumber);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //static Tuple<string, string, string, string> GetIndividualNumbers(string allNumbers)&nbsp; &nbsp; &nbsp; &nbsp; //{&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; string[] numbers&nbsp; = allNumbers.Split(' ');&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; return Tuple.Create<string,string,string,string>(numbers[0],numbers[1],numbers[2],numbers[3]);&nbsp; &nbsp; &nbsp; &nbsp; //}&nbsp; &nbsp; &nbsp; &nbsp; static object usingAnonymouseObject(string allNumbers)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] numbers = allNumbers.Split(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new { firstNumber = numbers[0], secondNumer = numbers[1], thirdNumber = numbers[2], fourthNumber = numbers[3] };&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static T Cast<T>(object obj, T type)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T)obj;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP