public class DelegateCodeDemo
public static void Main()
RecorderStats[] allRecorders = new RecorderStats[]
new RecorderStats("SWX01",10.5, 8000, 12),
new RecorderStats("SWX02",12.0, 1600, 10),
new RecorderStats("SWX03",23.6, 32000, 35),
new RecorderStats("SWX04",15.5, 900, 21),
new RecorderStats("SWX05",13.6, 8000, 3),
new RecorderStats("SWX06",5.1, 8000, 20),
new RecorderStats("SWX07",7.8, 8000, 40),
new RecorderStats("SWX08",21, 8000, 16),
new RecorderStats("SWX09",30.4, 8000, 18),
new RecorderStats("SWX10",6.5, 8000, 5)
Console.WriteLine(DisplayRecorders.DisplayResult(allRecorders));
public class DisplayRecorders
private delegate double DelegateTopValue(RecorderStats stats);
public static string DisplayResult(RecorderStats[] allRecorders)
string recorderWithLongestRecording = GetRecorderTopStats(allRecorders, stats => stats.RecordingLength_s);
string recorderWithHighestGain = GetRecorderTopStats(allRecorders, stats => stats.AudioGain_dB);
return "Recorder " + recorderWithLongestRecording + " has the longest recording time\n" +
"Recorder " + recorderWithHighestGain + " has the highest gain.";
private static string GetRecorderTopStats(RecorderStats[] allRecorders, DelegateTopValue statsChooser)
double longestRecording = 0;
foreach (RecorderStats stats in allRecorders)
double duration = statsChooser(stats);
if(duration > longestRecording)
longestRecording = duration;
name = stats.RecorderName;
public class RecorderStats
private string recorderName;
private double recordingLength_s;
private double samplingRate_Hz;
private double audioGain_dB;
public string RecorderName
get { return recorderName; }
public double RecordingLength_s
get { return recordingLength_s; }
public double SamplingRate_Hz
get { return samplingRate_Hz; }
public double AudioGain_dB
get { return audioGain_dB; }
public RecorderStats(string RecorderName, double RecordingLength_s, double SamplingRate_Hz, double AudioGain_dB)
this.recorderName = RecorderName;
this.recordingLength_s = RecordingLength_s;
this.samplingRate_Hz = SamplingRate_Hz;
this.audioGain_dB = AudioGain_dB;