mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
add topics for tuples to csharp/en
Added explanation and examples for tuples, deconstruction of tuples and other objects, and discarding unwanted fields during deconstruction
This commit is contained in:
parent
70c9d58bb1
commit
4eb33f98b4
@ -1106,23 +1106,63 @@ namespace Learning.More.CSharp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//New C# 7 Feature
|
||||||
|
//Install Microsoft.Net.Compilers Latest from Nuget
|
||||||
|
//Install System.ValueTuple Latest from Nuget
|
||||||
using System;
|
using System;
|
||||||
namespace Csharp7
|
namespace Csharp7
|
||||||
{
|
{
|
||||||
//New C# 7 Feature
|
// TUPLES, DECONSTRUCTION AND DISCARDS
|
||||||
//Install Microsoft.Net.Compilers Latest from Nuget
|
class TuplesTest
|
||||||
//Install System.ValueTuple Latest from Nuget
|
|
||||||
class Program
|
|
||||||
{
|
{
|
||||||
|
public (string, string) GetName()
|
||||||
|
{
|
||||||
|
// Fields in tuples are by default named Item1, Item2...
|
||||||
|
var names1 = ("Peter", "Parker");
|
||||||
|
Console.WriteLine(names1.Item2); // => Parker
|
||||||
|
|
||||||
|
// Fields can instead be explicitly named
|
||||||
|
// Type 1 Declaration
|
||||||
|
(string FirstName, string LastName) names2 = ("Peter", "Parker");
|
||||||
|
|
||||||
|
// Type 2 Declaration
|
||||||
|
var names3 = (First:"Peter", Last:"Parker");
|
||||||
|
|
||||||
|
Console.WriteLine(names2.FirstName); // => Peter
|
||||||
|
Console.WriteLine(names3.Last); // => Parker
|
||||||
|
|
||||||
|
return names3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetLastName() {
|
||||||
|
var fullName = GetName();
|
||||||
|
|
||||||
|
// Tuples can be deconstructed
|
||||||
|
(string firstName, string lastName) = fullName;
|
||||||
|
|
||||||
|
// Fields in a deconstructed tuple can be discarded by using _
|
||||||
|
var (_, last) = fullName;
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any type can be deconstructed in the same way by
|
||||||
|
// specifying a Deconstruct method
|
||||||
|
public int randomNumber = 4;
|
||||||
|
public int anotherRandomNumber = 10;
|
||||||
|
|
||||||
|
public void Deconstruct(out int randomNumber, out int anotherRandomNumber)
|
||||||
|
{
|
||||||
|
randomNumber = this.randomNumber;
|
||||||
|
anotherRandomNumber = this.anotherRandomNumber;
|
||||||
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
//Type 1 Declaration
|
var tt = new TuplesTest();
|
||||||
(string FirstName, string LastName) names1 = ("Peter", "Parker");
|
(int num1, int num2) = tt;
|
||||||
Console.WriteLine(names1.FirstName);
|
Console.WriteLine($"num1: {num1}, num2: {num2}"); // => num1: 4, num2: 10
|
||||||
|
|
||||||
//Type 2 Declaration
|
Console.WriteLine(tt.GetLastName());
|
||||||
var names2 = (First:"Peter", Last:"Parker");
|
|
||||||
Console.WriteLine(names2.Last);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user