thanks
do delegates need to be public methods?
Collapse
This topic is closed.
X
X
-
puzzlecrackerTags: None -
Jon Skeet [C# MVP]
Re: do delegates need to be public methods?
puzzlecracker <ironsel2000@gm ail.comwrote:No. For example:thanks
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
-
Duggi
Re: do delegates need to be public methods?
On Sep 22, 8:29 am, puzzlecracker <ironsel2...@gm ail.comwrote:not necessarily... look at the code below...thanks
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]
Re: do delegates need to be public methods?
Duggi <DuggiSrinivasa Rao@gmail.comwr ote:
<snip>
On the contrary, I'd say that most of the time delegates are createdhowever, 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).
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
Re: do delegates need to be public methods?
On Sep 22, 9:23 am, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:Hi Jon,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
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]
Re: do delegates need to be public methods?
Duggi <DuggiSrinivasa Rao@gmail.comwr ote:
<snip>
Could you give examples? In LINQ the delegates are almost alwaysThis 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.
specified as anonymous methods or lambda expressions - methods of
private classes or private methods.
Can you give a common example of where you'd provide a public method?May be this is my perception. Please correct me if I am wrong.
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
Comment