Code
/* Tuple class -> C# 4.0
* Value Tuple, deconstruction and discards from c# 7.0 */
//Tuple Class
//1.Useful to return multiple values from a method or to pass multiple values to a method.
//2.Represents a set of values....no class
//3.Tuple stores only a set of values(of any data type..); but doesnt store property names..
//4.Maximum 8 values can be stored..But if you further want to increase it -go for nested tuples...
//5.It is meaning less to pass such big number of parameters to Tuple...go for classes instead
Console.WriteLine("-----Tuple Basic example-----------");
var tuple = new Tuple(1, "hello");
Console.WriteLine(tuple.Item2);
//----------------------------------------------------------//
//Value Tuple --> c# 7.1
/*Simplified Syntax
1.No Item1..Item2 is needed unlike c#4.0
Older version of VS -> you need to install the package System.Value.Tuple but
VS >=2019 its inbuilt...
*/
Console.WriteLine("---------Tuple Old way C# 4.0-------------------------");
var player = IPLStars.GetIconPlayer();
Console.WriteLine(player.name);
Console.WriteLine(player.Team);
Console.WriteLine(player.player);
Console.WriteLine("---------Value Tuple C# 7.1-------------------------");
(string playerName, string Team, Player playerType) player1 = IPLStars.GetIconPlayer();
Console.WriteLine(player1.playerName);
Console.WriteLine(player1.Team);
Console.WriteLine(player1.playerType);
//Deconstruction
/*Assigns elements of value tuples into individual local variables.
Remove the reference variable...
Disadvantage-> order you need to take care of....
*/
Console.WriteLine("---------Deconstruction-------------------------");
//Treats playerName,team,playerType as local variables....
//you cannot create these variables in next lines as it is already declared
(string playerName, string team, Player playerType) = IPLStars.GetIconPlayer();
Console.WriteLine(playerName);
Console.WriteLine(team);
Console.WriteLine(playerType);
//might go wrong in this case...above is treated as local variables....
//(string team, string playername, Player playerType) = IPLStars.GetIconPlayer();
//Console.WriteLine(playerName);
//Console.WriteLine(team);
//Console.WriteLine(playerType);
Console.WriteLine("---------Deconstruction can messup as below-------------------------");
(string team1, string playername1, Player playerType1) = IPLStars.GetIconPlayer();
Console.WriteLine("Team is " + team1);
Console.WriteLine($"PlayerName is {playername1}");
Console.WriteLine(playerType);
Console.WriteLine("-------------------Discard-----------------------------------------");
//Discard allows you to skip a value which you dont require by using _
(string playerName2, _, Player playerType2) = IPLStars.GetIconPlayer();
Console.WriteLine(playerName2);
Console.WriteLine(playerType2);
public class IPLStars
{
public static (string name, string Team, Player player) GetIconPlayer()
{
return ("Hardik", "MI", Player.Icon);
}
}
public enum Player
{
Icon,
Legend,
Star,
SuperStar
}
OUTPUT
—–Tuple Basic example———–
hello
———Tuple Old way C# 4.0————————-
Hardik
MI
Icon
———Value Tuple C# 7.1————————-
Hardik
MI
Icon
———Deconstruction————————-
Hardik
MI
Icon
———Deconstruction can messup as below————————-
Team is Hardik
PlayerName is MI
Icon
——————-Discard—————————————–
Hardik
Icon