25 Şubat 2018 Pazar

C# 7.0 Discards

Giriş
Açıklaması şöyle
Discards are local variables which you can assign but cannot read from. i.e. they are “write-only” local variables.
Örnek
Şöyle yaparız.
if (bool.TryParse("TRUE", out bool _))
Örnek
Tuple dönen bir metod için şöyle yaparız.
public static void Main()
{
    var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
    Console.WriteLine($"Population change, 1960 to 2010: {pop2 - pop1:N0}");
}

private static (string, double, int, int, int, int)
  QueryCityDataForYears(string name, int year1, int year2)
{
  int population1 = 0, population2 = 0;
  double area = 0;

  if (name == "New York City")
  {
     area = 468.48;

     if (year1 == 1960) {
       population1 = 7781984;
    }

    if (year2 == 2010) {
      population2 = 8175133;
    }

    return (name, area, year1, population1, year2, population2);
  }

  return ("", 0, 0, 0, 0, 0);
}

Hiç yorum yok:

Yorum Gönder