如何重命名 return 个元组项?
How to rename return Tuples Items?
我是 C# 新功能 Called as Tuples 的新手。我在元组上写了一些代码。当元组是 return 方法时(元组包含多个值)。
即
string firstNumber = separateNumbers.Item1;
string secondNumber = separateNumbers.Item2;
string thirdNumber = separateNumbers.Item3;
string fourthNumber = separateNumbers.Item4;
在真实场景中,识别哪个项目包含哪个值变得很复杂?
所以我想提供一些特定的名称,而不是 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);
}
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]);
}
}
}
我进一步研究了这个问题,得出了结论。
结论:
关于问题:
So I want to provide some specific name instead of item1,item2, etc...
How to do that ?
- 元组不允许我们重命名 return 值相反我们可以利用
匿名类型.
代码:
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;
}
}
}
C# 7.0 tuples feature uses ValueTuple, not the old 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]);
}
}
}
我是 C# 新功能 Called as Tuples 的新手。我在元组上写了一些代码。当元组是 return 方法时(元组包含多个值)。
即
string firstNumber = separateNumbers.Item1;
string secondNumber = separateNumbers.Item2;
string thirdNumber = separateNumbers.Item3;
string fourthNumber = separateNumbers.Item4;
在真实场景中,识别哪个项目包含哪个值变得很复杂?
所以我想提供一些特定的名称,而不是 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);
}
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]);
}
}
}
我进一步研究了这个问题,得出了结论。
结论:
关于问题:
So I want to provide some specific name instead of item1,item2, etc... How to do that ?
- 元组不允许我们重命名 return 值相反我们可以利用 匿名类型.
代码:
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;
}
}
}
C# 7.0 tuples feature uses ValueTuple, not the old 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]);
}
}
}