473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can you do a BinarySearch with an anonymous method?

I can do the following with Find:

Racer theRacer2 = racers.Find(delegate(Racer racer) { return racer.Car ==
"Ferrari"; });

But I can't seem to do the same with BinarySearch, this:

int iktr = racers.BinarySearch(delegate(Racer racer) { return racer.Car ==
"Ferrari"; });

gives me these errors:

The best overloaded method match for
'System.Collections.Generic.List<Generics.Racer>.B inarySearch(Generics.Racer)'
has some invalid arguments

and

Argument '1': cannot convert from 'anonymous method' to 'Generics.Racer'

Can I make this work with an anonymous method or do I need to create another
function to use BinarySearch?

Thanks,

Tom
Mar 11 '08 #1
2 2161
Marc Gravell wrote:
Here you go - a C# 3 version amenable to binary-search on a sub-
property; actually it should work in C# 2 given a few minor changes,
but anonymous methods aren't as appealing as lambdas, and the
extension methods and improved type inference really help us here...
There's a problem using BinarySearch like that... it's fragile. The list
has to be sorted by the same comparer used for binary search.

Instead, use a SortedList, where the comparer is kept with the list and
can't become inconsistent between sorting and searching.
Mar 11 '08 #2
There's a problem using BinarySearch like that... it's fragile. *The list
has to be sorted by the same comparer used for binary search.
Yes, but it is hard to get around that and keep with a simple List<T>;
changing to SortedList<TKey,TValueisn't necessarily trivial,
especially if you to allow different sorting (which may necessitate
different TKey).

Or put another way - it doesn't make List<T>.BinarySearch any more or
less brittle - it just makes it possible to call it without having to
write your own IComparer<Teach time.
Mar 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
7
by: Rene | last post by:
We all know that we can't call custom methods or properties form generic type parameters (<T>) by default because the compiler will complain about his. For example, the following won't normally...
7
by: moondaddy | last post by:
I'm reading the book Wrox-Professional C# 2005 and it's trying to demonstrate an anonymous method in an event handler. I would not do this in a real app, but am trying to understand how this...
7
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
1
by: Barry Mossman | last post by:
Hi, is it possible to detach an event which is implemented via an anonymous method. ie. If the code is: myObject.myEvent += delegate(object sender, EventArgs e) { // snip event actions } is...
3
by: Nick | last post by:
Hi, I was just playing around and found that the following two ways of calling the same method using the WaitCallback method that takes an object parameter. Can any one explain why there is no...
3
by: William Stacey [C# MVP] | last post by:
It would be handy to be able to ref "this" from inside an AM such as: (string s) { Console.Writeline(s); DoSomething(this); } So treating am like a method of a class (which it is)....
2
by: Bob | last post by:
What name does the compiler give to anonymous methods in the output assembly - or what is the "name pattern"? .... can't imagine it would be relevant to anything I do... just curious.
3
by: Robert Howells | last post by:
Is there any such thing as an "anonymous delegate"? I understand what an anonymous method is, but I'm unclear of the concept of anonymous delegate (if it is even a valid term to be using). Is an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.