473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

do delegates need to be public methods?

thanks
Sep 22 '08 #1
5 1182
puzzlecracker <ir*********@gmail.comwrote:
thanks
No. For example:

using System;

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

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

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #2
On Sep 22, 8:29*am, puzzlecracker <ironsel2...@gmail.comwrote:
thanks
not necessarily... look at the code below...

delegate void myDelegate(int a);

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

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

Console.ReadLine();
}

}

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
Sep 22 '08 #3
Duggi <Du***************@gmail.comwrote:

<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.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #4
On Sep 22, 9:23*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Duggi <DuggiSrinivasa...@gmail.comwrote:

<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.com>
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

Sep 22 '08 #5
Duggi <Du***************@gmail.comwrote:

<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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
10
by: Brad | last post by:
Question for all: Would one consider this a loophole in .net with respect to nested (inner) classes? 1) Create outer class Foo and create a nested class Yin (inside Foo's class definition...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
15
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
4
by: Jarod_24 | last post by:
What is the point with Delegates in VB.Net What can these things do that we can not allready do with the use of Interfaces, Events and Event handlers and so on... I'd like a discussion on this,...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
13
by: heddy | last post by:
Using Jesse Liberty's excellent book on C#, I am looking at chapter 12 - Delegates. Now, I was under the impression that a delegate is essentially a pointer to a member function. However, when I...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.