locked
What is the main usage of Delegate RRS feed

  • Question

  • User264732274 posted

    i have seen people use Delegate to call function but without Delegate we can call any function directly......then why Delegate is used in code?

    like to see couple of real usage of delegate in program where people has to use Delegate. give few example of real usage of Delegate.

    what is predicate and how it is different than Delegate...........need some example to understand predicate and also make me understand how it is different than delegate providing some code. thanks

    Tuesday, June 9, 2015 7:35 AM

Answers

  • User724169276 posted

    May be you should check some articles and try to understand first : http://csharpindepth.com/Articles/Chapter2/Events.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2015 7:39 AM
  • User753101303 posted

    Hi,

    A link to the code you saw? It would be easier to comment knowing what they have done exactly (especially if it was really not needed).

    It is basically a typed function pointer so you can write some code without knowing yet what is the actual function that will be ultimately called. For example Array.ForEach and Array.FindAll allows to process or select array items by having the user prodived just the function that should  be used. Else you would have to explicitely loop again and again, the only differencve behind the function applied inside the loop.

    NET already defines a bunch of delegate types for different usage. A predicate is just a function that returns a boolean (so in IT it is often used when dealing with WHERE clauses, filtering from a list etc...).

    In most if not all cases, you'll juste use already defined delegates and delegate based code (I don't remember the last time I had too).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2015 8:43 AM
  • User724169276 posted

    where people has to use Delegate and has no other way.

    Thats not a proper line , its upto the developers how they want to implement delegates,BUT its not compulsary to use it everywhere.Read this :

    http://www.codeproject.com/Articles/85296/important-uses-of-Delegates-and-Events

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2015 4:05 AM
  • User753101303 posted

    The first place where you'll see that is with events:

    https://msdn.microsoft.com/en-us/library/w369ty8x.aspx (events are basically enhanced delegates, here as well the purpose is to call into user defined code from within some previously written code). See also https://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx for a code sample.

    As for any other feature, the pojnt is rather to understand what it does so that you can use it if you are trying to do that. So same for example than Array.ForEach etc... that is when writing some code that needs to call into some code that will be provided later (which should be quite rare).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2015 5:34 AM

All replies

  • User724169276 posted

    May be you should check some articles and try to understand first : http://csharpindepth.com/Articles/Chapter2/Events.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2015 7:39 AM
  • User753101303 posted

    Hi,

    A link to the code you saw? It would be easier to comment knowing what they have done exactly (especially if it was really not needed).

    It is basically a typed function pointer so you can write some code without knowing yet what is the actual function that will be ultimately called. For example Array.ForEach and Array.FindAll allows to process or select array items by having the user prodived just the function that should  be used. Else you would have to explicitely loop again and again, the only differencve behind the function applied inside the loop.

    NET already defines a bunch of delegate types for different usage. A predicate is just a function that returns a boolean (so in IT it is often used when dealing with WHERE clauses, filtering from a list etc...).

    In most if not all cases, you'll juste use already defined delegates and delegate based code (I don't remember the last time I had too).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2015 8:43 AM
  • User264732274 posted

    @PatriceSc i asked.....like to see couple of real usage of delegate in program where people has to use Delegate and has no other way.

    can u plzz give some few real life example of delegate. thanks

    Wednesday, June 10, 2015 3:59 AM
  • User724169276 posted

    where people has to use Delegate and has no other way.

    Thats not a proper line , its upto the developers how they want to implement delegates,BUT its not compulsary to use it everywhere.Read this :

    http://www.codeproject.com/Articles/85296/important-uses-of-Delegates-and-Events

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2015 4:05 AM
  • User753101303 posted

    The first place where you'll see that is with events:

    https://msdn.microsoft.com/en-us/library/w369ty8x.aspx (events are basically enhanced delegates, here as well the purpose is to call into user defined code from within some previously written code). See also https://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx for a code sample.

    As for any other feature, the pojnt is rather to understand what it does so that you can use it if you are trying to do that. So same for example than Array.ForEach etc... that is when writing some code that needs to call into some code that will be provided later (which should be quite rare).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 10, 2015 5:34 AM