$ 0 0 only note events can be cast to NoteEvent. So only cast if you get note.CommandCode == MidiCommandCode.NoteOn)