Create Tuple type to accept return value - CSharp Language Basics

CSharp examples for Language Basics:Tuple

Description

Create Tuple type to accept return value

Demo Code

using static System.Console;
using System;//from   www  .ja v  a  2s  .c  o  m
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      var p1 = new Person();
      (string fruitName, int fruitNumber) = p1.GetFruitCS7();
      WriteLine($"Deconstructed: {fruitName}, {fruitNumber}");
   }
}
public class Person : object
{
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
   public readonly DateTime Instantiated;
   public const string Species = "Programmer";
   public readonly string HomePlanet = "Earth";
   public Person()
   {
      // set default values for fields
      // including read-only fields
      Name = "Unknown";
      Instantiated = DateTime.Now;
   }
   public Person(string initialName)
   {
      Name = initialName;
      Instantiated = DateTime.Now;
   }
   // the new C# 7 syntax and new System.ValueTuple type
   public (string Name, int Number) GetFruitCS7()
   {
      return (Name: "Apples", Number: 5);
   }
}

Result


Related Tutorials