top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can we declare Delegate inside the class?

+1 vote
606 views
Can we declare Delegate inside the class?
posted May 28, 2014 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Partially Yes. Remember about delegates is that they are in themselves classes which inherit from System.Delegate. Since nested classes are allowed in classes you my declare a delegate in a class.

Following is a valid in C#:

public class MyClass
{
   public delegate void MyDelegate();
}

To reference that delegate outside of MyClass you would write the following:

MyClass.MyDelegate myDel;

Also to remember that a delegate is a type that inherits from delegate you cannot declare a delegate within an interface, because you cannot declare nested types in an interface.

answer May 28, 2014 by Salil Agrawal
Similar Questions
...