do delegates need to be public methods?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    do delegates need to be public methods?

    thanks
  • Jon Skeet [C# MVP]

    #2
    Re: do delegates need to be public methods?

    puzzlecracker <ironsel2000@gm ail.comwrote:
    thanks
    No. For example:

    using System;

    public class Test
    {
    static void Main(string[] args)
    {
    Action action = Foo;
    action();
    }

    private static void Foo()
    {
    Console.WriteLi ne("Foo!");
    }
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Duggi

      #3
      Re: do delegates need to be public methods?

      On Sep 22, 8:29 am, puzzlecracker <ironsel2...@gm ail.comwrote:
      thanks
      not necessarily... look at the code below...

      delegate void myDelegate(int a);

      class Csharp
      {
      private void DelegateMethod( int i)
      {
      Console.WriteLi ne("Not exactly");
      }

      static void Main()
      {
      Csharp cs = new Csharp();
      myDelegate del = new myDelegate(cs.D elegateMethod);
      del(10);

      Console.ReadLin e();
      }

      }

      however, the above is not a practical case. Generally delegates used
      for representing some functionality that is expected out of other
      objects.. or functionality that would be abstracted to third party. In
      which case access to the method is exactly required. (most of the
      times through public modifier).

      -Cnu


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: do delegates need to be public methods?

        Duggi <DuggiSrinivasa Rao@gmail.comwr ote:

        <snip>
        however, the above is not a practical case. Generally delegates used
        for representing some functionality that is expected out of other
        objects.. or functionality that would be abstracted to third party. In
        which case access to the method is exactly required. (most of the
        times through public modifier).
        On the contrary, I'd say that most of the time delegates are created
        from private methods. Just because a button needs to know what to do
        when it's clicked doesn't mean that method has to be public - it can be
        private to the class which is subscribing to the event, and that's
        usually the case.

        Likewise anonymous methods and lambda expressions generate private
        methods or methods within private nested classes.

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • Duggi

          #5
          Re: do delegates need to be public methods?

          On Sep 22, 9:23 am, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
          Duggi <DuggiSrinivasa ...@gmail.comwr ote:
          >
          <snip>
          >
          however, the above is not a practical case. Generally delegates used
          for representing some functionality that is expected out of other
          objects.. or functionality that would be abstracted to third party. In
          which case access to the method is exactly required. (most of the
          times through public modifier).
          >
          On the contrary, I'd say that most of the time delegates are created
          from private methods. Just because a button needs to know what to do
          when it's clicked doesn't mean that method has to be public - it can be
          private to the class which is subscribing to the event, and that's
          usually the case.
          >
          Likewise anonymous methods and lambda expressions generate private
          methods or methods within private nested classes.
          >
          --
          Jon Skeet - <sk...@pobox.co m>
          Web site:http://www.pobox.com/~skeet 
          Blog:http://www.msmvps.com/jon.skeet
          C# in Depth:http://csharpindepth.com
          Hi Jon,

          Please correct me, if I am wrong...

          I think,

          On the contrary, I'd say that most of the time delegates are created
          from private methods. Just because a button needs to know what to do
          when it's clicked doesn't mean that method has to be public - it can
          be
          private to the class which is subscribing to the event, and that's
          usually the case.

          This is the case usually with the events (of course a type of
          delegate). However the general use case of delegate is to expect the
          functionality from some one outside the object, in which case delegate
          method becomes public.

          May be this is my perception. Please correct me if I am wrong.

          -Cnu

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: do delegates need to be public methods?

            Duggi <DuggiSrinivasa Rao@gmail.comwr ote:

            <snip>
            This is the case usually with the events (of course a type of
            delegate). However the general use case of delegate is to expect the
            functionality from some one outside the object, in which case delegate
            method becomes public.
            Could you give examples? In LINQ the delegates are almost always
            specified as anonymous methods or lambda expressions - methods of
            private classes or private methods.
            May be this is my perception. Please correct me if I am wrong.
            Can you give a common example of where you'd provide a public method?
            There are times when that would be useful, of course, but I think it
            would be quite rare.

            The functionality can be provided to a class separate from the one
            creating the delegate, but in my experience the logic within the
            delegate is *usually* private to the class creating the delegate.

            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            Working...