locked
delegates and events RRS feed

  • Question

  • User-778131909 posted

    1. what makes delegates favourite choice for event implementations.

    2. is there an alternative way for implementing event i.e; without using delegates.

    3. just wanted to see some examples where delegates being the best fit, other than being used for events.

    Saturday, October 18, 2014 8:31 PM

Answers

  • User-434868552 posted

    @VickySan

    in the example that i gave where one wants a unique way of putting data in an special order ("List<T>.Sort Method (Comparison<T>)"), a delegate must be used because the designer(s) of  List<T>.Sort chose to allow you to use a delegate.

    an entirely different approach would need to be taken for List<T>.VickySanSort, for example, you might have your VickySanSort take a parameter List<String> where the consumer of VickySanSort must present a List<String> of rules, using a syntax that you specified to specify rules similar to my example at http://forums.asp.net/post/5795277.aspx above.

    VickySan

    What I am looking at is a real world example where delegate are used and alternative to implement the same and how delegate has edge over other implementation.

    also, in the idea that i've just presented above, the delegate in the existing .NET Framework method allows the consumer programmer to create her/his own method without having to learn your rules in the hypothetical VickySanSort rules method; additionally, the existing .NET Framework method therefore allows the consumer programmer to create her/his own method to cover cases for which your VickySanSort rules might be inadequate.

    imho, it's fair to claim that delegates are simply far more powerful solutions when great flexibility is highly desirable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2014 10:38 AM

All replies

  • User-434868552 posted

    @VickySan i recommend that you study the following articles; if c# is your only language, the idea of delegates may be totally new to you. 

    http://msdn.i microsoft.com/en-us/library/ms173172.aspx "Using Delegates (C# Programming Guide)"

    for questions like this, it's a good idea to use Google "c# reasons to use delegates".

    You will find there is a lot of material ... after reading through the material, you will be better equipped to ask your questions, plus you will already have answered some of these questions yourself.

    one example is the use of delegates in custom comparison, for example, see http://msdn.microsoft.com/en-us/library/w56d4y5z(v=vs.110).aspx "List<T>.Sort Method (Comparison<T>)".  ==> "If comparison is provided, the elements of the List<T> are sorted using the method represented by the delegate." ~~ imagine you wanted a method where you wanted to place items of a list in a specific, non-alphabetic ordering, for example, vowels first, followed by consonants, with small consonants c, m, n, ... followed by full height consonants b, d, f, ... followed by consonants with descenders  g, j, p, ...        .... 

    Sunday, October 19, 2014 12:05 AM
  • User-40287846 posted

    The below site may help you out

    http://msdn.microsoft.com/en-us/library/orm-9780596516109-03-09.aspx

    Tuesday, October 21, 2014 5:07 PM
  • User-778131909 posted

    Hi Gerry,

        Thanks for your reply

         I have gone through different articles to get at idea about what is delegate, and it usage...., Some one pointed out delegate can be used as an example for a calculator class where each of mathematical functions accepts same number of parameters and have same return type and using delegate we can call any of the function by passing the function name indicator and fetch a particular function, which I feel can be done by normal function and calling based on different condition.

    or many of the articles goes on to explaining event implementation as delegates usage.

    May be a clear real world example of delegate will make it more understandable. o.w even after going through many application I will not be able to apply delegate when required and go-on some other way which may be time consuming.

    What I am looking at is a real world example where delegate are used and alternative to implement the same and how delegate has edge over other implementation.

    Wednesday, October 22, 2014 4:42 AM
  • User-434868552 posted

    @VickySan

    in the example that i gave where one wants a unique way of putting data in an special order ("List<T>.Sort Method (Comparison<T>)"), a delegate must be used because the designer(s) of  List<T>.Sort chose to allow you to use a delegate.

    an entirely different approach would need to be taken for List<T>.VickySanSort, for example, you might have your VickySanSort take a parameter List<String> where the consumer of VickySanSort must present a List<String> of rules, using a syntax that you specified to specify rules similar to my example at http://forums.asp.net/post/5795277.aspx above.

    VickySan

    What I am looking at is a real world example where delegate are used and alternative to implement the same and how delegate has edge over other implementation.

    also, in the idea that i've just presented above, the delegate in the existing .NET Framework method allows the consumer programmer to create her/his own method without having to learn your rules in the hypothetical VickySanSort rules method; additionally, the existing .NET Framework method therefore allows the consumer programmer to create her/his own method to cover cases for which your VickySanSort rules might be inadequate.

    imho, it's fair to claim that delegates are simply far more powerful solutions when great flexibility is highly desirable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2014 10:38 AM