|
|
Dekonstruktoren für Klassen
A05.cs
//===========================================================
// Class deconstructors
//===========================================================
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 void Deconstruct(out int id, out string lastName) {
id = this.Id;
lastName = this.LastName;
}
public void Deconstruct(out int id, out string firstName, out string lastName) {
id = this.Id;
firstName = this.FirstName;
lastName = this.LastName;
}
}
public class A5 {
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")
};
//--- Print students using deconstructor 1
foreach (Student student in students) {
(int id, string lastName) = student;
Console.WriteLine(id + ": " + lastName);
}
Console.WriteLine();
//--- Print students using deconstructor 2
foreach (Student student in students) {
(int id, string firstName, string lastName) = student;
Console.WriteLine(id + ": " + firstName + " " + lastName);
}
}
}
|
|