I have a book that says:-
a delegate may be defined in the following places:-
1) Inside a class - ok!
2) Outside all classes - ok!
3) As a top level object in a namespace - huh??
please can some1 throw light on 3rd?
Printable View
I have a book that says:-
a delegate may be defined in the following places:-
1) Inside a class - ok!
2) Outside all classes - ok!
3) As a top level object in a namespace - huh??
please can some1 throw light on 3rd?
Perfectly legal. You can see the Delegate like a blueprint ( or like a class ) . First you declare it and then you instantiate it. You don't need to declare it in a class.
Code:namespace WindowsApplication1
{
publicdelegatevoidMyDelegate();
publicpartialclassForm1 : Form
{
MyDelegate del;
privatevoid DoNothing()
{
MessageBox.Show("nothing");
}
public Form1()
{
InitializeComponent();
del = newMyDelegate(DoNothing);
}
}
}
thanx !