Last post Jun 10, 2015 05:34 AM by PatriceSc
Jun 09, 2015 07:35 AM|sudip_inn|LINK
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
Jun 09, 2015 07:39 AM|Ashim Chatterjee|LINK
May be you should check some articles and try to understand first : http://csharpindepth.com/Articles/Chapter2/Events.aspx
Jun 09, 2015 08:43 AM|PatriceSc|LINK
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).
Jun 10, 2015 03:59 AM|sudip_inn|LINK
@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
Jun 10, 2015 04:05 AM|Ashim Chatterjee|LINK
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 :
Jun 10, 2015 05:34 AM|PatriceSc|LINK
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).