Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need More Info On Delegates
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01385016
Message ID:
01385025
Views:
41
>I'm looking at this example: http://geekswithblogs.net/joycsharp/archive/2008/02/15/simple-c-delegate-sample.aspx
>
>It's clear and short, and I get the syntax, but the question is why.
>
>Since class A is creating an instance of class B, why would I not just write a switch statement that calls either the Add or the Sub method?
>
>I guess I'm still searching for a real-world use for delegates.

Well, you've seen how they're used with events. They have the same use here (although the example you looked at didn't really make this clear). In cases where Class A knows specifically about methods on Class B (and you don't mind the classes being tightly coupled) you could just use a switch. But lets suppose that Class A doesn't know about Class B (but B knows about A). How does A call a method on B? Delegates let you do that.

A real example from the framework: regular expressions. I want to use .NET's regex engine to find every occurance of something and then let me replace it with something else when it finds a match. How would you write code that does this? You could actually just raise an event (and let the calling class subscribe to it) and fire it on each event (let's ignore the fact that it's being done by delegates). That would work OK. You could, instead, pass in a delegate directly and let the regex class fire your method directly.

Here's a simple example: we're going to search the content string and replace anything that looks like an HTML tag (a less than < and a greater than > ) and replace it with spaces.

Here's what it might look like:
private static string PadMatch(Match match)
{
    return " ".PadRight(match.Length);
}

public string StripHTML(content)
{ 
    string newSentence = Regex.Replace(content, 
                                     @"<(.|\n)*?>",
                                     PadMatch);
    return newSentence;
}
This example is using an "anonymous delegate". We basically can embed code that would normally need to be in a separate method inline. It makes the code a bit simpler in some cases:
string newSentence = Regex.Replace(content, 
                                 @"<(.|\n)*?>",
                                 delegate(Match match)
                                 {
                                     return " ".PadRight(match.Length);
                                 });
And finally here's the same thing using the new lambda syntax added for LINQ:
string newSentence = Regex.Replace(content, 
                                @"<(.|\n)*?>", 
                                match => " ".PadRight(match.Length));                        
-Paul

RCS Solutions, Inc.
Blog
Twitter
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform