Return System.Tuple from method - CSharp Language Basics

CSharp examples for Language Basics:Tuple

Description

Return System.Tuple from method

Demo Code

using static System.Console;
using System;//w w  w  .ja  va 2s  . co m
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      var p3 = new Person();
      WriteLine($"{p3.Name} was instantiated at  {p3.Instantiated:hh:mm:ss} on {p3.Instantiated:dddd, d MMMM  yyyy}");
      Tuple<string, int> fruit4 = p3.GetFruitCS4();
      WriteLine($"There are {fruit4.Item2} {fruit4.Item1}.");
   }
}
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()
   {
      Name = "Unknown";
      Instantiated = DateTime.Now;
   }
   public Person(string initialName)
   {
      Name = initialName;
      Instantiated = DateTime.Now;
   }
   // the old C# 4 and .NET 4.0 System.Tuple type
   public Tuple<string, int> GetFruitCS4()
   {
      return Tuple.Create("Apples", 5);
   }
}

Result


Related Tutorials