4.5. Script Blocks as Delegates

Delegates are an integral part of .NET's event handling mechanism. In short, delegates are objects that keep a reference to a .NET class, a method reference, and an object reference if the method is not static. When a delegate is invoked, it will call the method of the object it references. Events in .NET are implemented as delegates pointing to the event receiver object and method, and passing those delegates to the event sender. The sender invokes the event delegates when it raises the event, and the receiver gets notified.

Script blocks and delegates have a lot in common—both of them point to some code and can be invoked to execute that code. PowerShell's type system allows a script block to be converted to ...

Get Pro Windows PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.