Home   Cover Cover Cover Cover
 

Tupel als Typparameter

A02.cs
//===========================================================
// Tuples as type parameters
// -------------------------
// Build a list of books.
// Convert it into a list of tuples (author, title).
// Print the tuples.
//===========================================================

using System;
using System.Collections.Generic;

class Book {
  public string author;
  public string title;
  public string publisher;
  public int year;
  public Book(String a, String t, String p, int y) {
    author = a; title = t; publisher = p; year = y;
  }
}

public class A2 {
  
  public static void Main() {
    
    //--- Build a list of books
    List list = new List();
    list.Add(new Book("Knuth", "The Art of Computer Programming", "Addison-Wesley", 1971));
    list.Add(new Book("Dijkstra", "A Discipline of Programming", "Prentice-Hall", 1976));
    list.Add(new Book("Wirth", "Pascal User Manual and Report", "Springer", 1984));
    list.Add(new Book("Clarke", "Handbook of Model Checking", "Springer", 2018));

    //--- Convert it into a list of tuples    
    var list2 = new List<(string author, string title)>();
    foreach (Book b in list) {
      list2.Add((b.author, b.title));
    }
    
    //--- Print the tuples
    foreach (var elem in list2) {
      Console.WriteLine(elem.author + ": " + elem.title);
    }
  }
}