Can a delegate instance be defined outside of a class?

324 views Asked by At

From what I am reading, a delegate instance is always defined with a class as an input or inside a class.

Why can't I define a delegate instance independently?

1

There are 1 answers

0
Ben Voigt On BEST ANSWER

A delegate is a type, and you can define it at namespace scope (including the global namespace).

Since delegates are reference types, delegate instances always are placed on the managed (garbage collected) heap. Delegate instances can be created with the gcnew operator, the Delegate::CreateDelegate method, or using stack semantics syntax (C++/CLI only).

A reference variable of delegate type (including stack semantics syntax variables which wrap a permanently-bound reference, an instance on the heap, and an automatic call to IDisposable::Dispose) can exist as an instance or static member of a managed type, an automatic local variable, a static local variable, or (in C++/CLI) as a global (namespace scope) variable.