Sunday, April 24, 2016

tuples just keep getting better

This C# 4.0 way of doing things...

public Tuple<string, int> GetSayingAndSubstance()
{
   return new Tuple<string, int>("lucky",13);
}

 
 

...may now take a shape like so in C# 7.0!

public (string Saying, int Substance) GetSayingAndSubstance()
{
   return new (string Saying, int Substance)
      {
         Saying = "lucky",
         Substance = 13
      };
}

 
 

This means that this in the C# 4.0 way of doing things...

var x = GetSayingAndSubstance();
var y = x.Item1;
var z = x.Item2;

 
 

...may be reshaped like so the C# 7.0 approach to be more elegant:

var x = GetSayingAndSubstance();
var y = x.Saying;
var z = x.Substance;

No comments:

Post a Comment