FlaZeR (6) [Avatar] Offline
#1
The text suggests, rather than using:

delegate Boolean FilterDelegate(Process process);

to instead use the Predicate<T> syntax... no issue there...

However, how would the declared version be implemented?
fabrice.marguerie (224) [Avatar] Offline
#2
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
Just use Predicate<Process>, a predicate that applies to a Process object.
FlaZeR (6) [Avatar] Offline
#3
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
Thanks - as i said, i understood that using Predicate<T> was the way to go.

I am however curious regards the "old shool" approach.
fabrice.marguerie (224) [Avatar] Offline
#4
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
I'm afraid I don't understand your question.
What do you want to know exactly?
FlaZeR (6) [Avatar] Offline
#5
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
Assuming Predicate<T> did not exist, how would the example work with:

delegate Boolean FilterDelegate(Process process);

??
fabrice.marguerie (224) [Avatar] Offline
#6
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
The method in Listing 2.7 would be as follow:

[pre]static void DisplayProcesses(FilterDelegate match)
{
...
}[/pre]
FlaZeR (6) [Avatar] Offline
#7
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
Thank you Fabrice - you nailed it!
FlaZeR (6) [Avatar] Offline
#8
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
Fabrice, why does this not work?


delegate Boolean FilterDelegate(Process process);
static bool Filter(Process process)
{
return process.WorkingSet64 > 1024 * 1024 * 20;
}
static void Main()
{
DisplayProcesses(Filter);
DisplayProcesses(process => process.WorkingSet64 >= 1024 * 1024 * 20);
}

static void DisplayProcesses(FilterDelegate match)
{
DisplayProcessesNow(match);
}

static void DisplayProcessesNow(Predicate<Process> match)
{
...
}


Neither DISPLAYPROCESS call to DISPLAYPROCESSNOW works... am i missing a cast?
fabrice.marguerie (224) [Avatar] Offline
#9
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
You'll find the explanation in the following article: http://code.logos.com/blog/2008/07/casting_delegates.html

"One of the annoying things about delegates in .NET is that delegates with exactly the same parameters and return type are not compatible. Specifically, you cannot cast a delegate to a delegate of another type even if they have the same parameters and return type."

Concretely, in your case, FilterDelegate and Predicate<Process> are two different types. They have no relation to each other and cannot be cast one to the other.

[pre]FilterDelegate x = Filter;
Predicate<Process> y = Filter;
x = y; // ERROR: Cannot implicitly convert type 'System.Predicate<System.Diagnostics.Process>' to 'FilterDelegate'[/pre]
The easiest way to solve this is to use the following:

[pre]FilterDelegate x = Filter;
Predicate<Process> y = Filter;
x = p => y(p);[/pre]
Fabrice
FlaZeR (6) [Avatar] Offline
#10
Re: Second printing, Chapter 2, pg 57, Delegate Declaration, used how?
Thanks Fabrice - I suppose that makes sense since each are independent pointers and it's fair to reason, each resolve to a different memory location.