2

in fact,What's the difference when we define delegate in the class or out of it ?I am a Beginner, please explain simple.

Example 1

namespace Delegate
{
    public delegate int Calculate(int value1, int value2);
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Example 2

class Program
{
    public delegate int Calculate(int value1, int value2);
    static void Main(string[] args)
    {
    }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
behnam
  • 1,095
  • 2
  • 11
  • 30
  • 1
    http://stackoverflow.com/questions/6189289/why-can-we-declare-delegates-outside-a-class-is-it-not-against-oop-concept – diiN__________ Mar 14 '16 at 13:11
  • In short your answer would be you could repeatedly use the same delegate template in many classes rather than just the one – BugFinder Mar 14 '16 at 13:13
  • @Alex,http://csharpindepth.com/Articles/Chapter2/Events.aspx – behnam Mar 14 '16 at 13:14
  • 3
    its like having nested class in class. if the delegate is just related to that class then write it in, if not then write it out side. no difference in practice. – M.kazem Akhgary Mar 14 '16 at 13:16
  • 3
    You should use the first approach, [according to Micirosoft's guidelines.](https://msdn.microsoft.com/en-us/library/ms182162.aspx). (A delegate is a `type`, so it should not be nested unless it is private or protected.) – Matthew Watson Mar 14 '16 at 13:22

1 Answers1

3

What's the difference when we define delegate in the class or out of it

When you define a delegate outside the class, and in no other class, it belongs to the namespace it was defined on. This means that if one wanted to use this delegate, the full path to it would look like this:

namespace ConsoleApplication1
{
    class Program
    {
        static void H()
        {
            MyDelegate.Calculate y = (x, w) => 1;
        }
    }
}

namespace MyDelegate
{
    public delegate int Calculate(int value1, int value2);
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

As opposed if you were to expose it inside a class, your path would contain the actual class that defined the delegate:

namespace ConsoleApplication1
{
    class Program
    {
        static void H()
        {
            MyDelegate.Program.Calculate y = (x, w) => 1;
        }
    }
}

namespace MyDelegate
{   
    class Program
    {
        public delegate int Calculate(int value1, int value2);
        static void Main(string[] args)
        {
        }
    }
}

Edit:

As @MatthewWatson points out in the comments, as delegate is a type, it should usually be declared at the namespace level, like Action<T> and Func<T> are in the .NET framework. If you want to implement a delegate which will internally be used inside a class hierarchy, you could potentially place it inside the class and set it as protected. I do recommend using the delegates mentioned above which are exposed by the .NET framework and fulfill most requirements.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321