Home   Cover Cover Cover Cover
 

Geschachtelte Tupel

A04.cs
//===========================================================
// Nested tuples
// -------------
// Create a list of students.
// Convert it into a list of nested tuples.
// Print them.
//===========================================================

using System;
using System.Collections.Generic;

class Student {
  public int Id {get; set;}
  public string FirstName {get; set;}
  public string LastName {get; set;}
  public string Field {get; set;}
  
  public Student(int Id, string FirstName, string LastName, string Field) {
    this.Id = Id;
    this.FirstName = FirstName;
    this.LastName = LastName;
    this.Field = Field;
  }
}

public class A4 {
  
  public static void Main() {
    
    //--- Create a list of Students
    List students = new List {
      new Student(1855367, "John", "Doe", "Computing"),
      new Student(1855201, "Mary", "Miller", "Mathematics"),
      new Student(1855553, "Jane", "Jenkins", "Computing"),
      new Student(1855176, "Jeff", "Joplin", "Computing"),
      new Student(1855285, "Alex", "Adelson", "Mathematics")
    };
    
    //--- Convert it into a nested list of tuples
    var tuples = new List<(int Id, (string First, string Last), string Field)>();
    foreach (Student student in students) {
      tuples.Add((student.Id, (student.FirstName, student.LastName), student.Field));
    }
    
    //--- Print the tuples
    foreach (var tuple in tuples) {
      var name = tuple.Item2;
      Console.WriteLine(tuple.Id + ": " + name.First + " " + name.Last + " studies " + tuple.Field);
    }
  }
}