using anonymous methods

  • Thread starter Thread starter Alexandre
  • Start date Start date
A

Alexandre

cross post:

Hi can someone justify the use of these anonymous methods in C# 2.0 &
3.0 ?
I simply do not see a use for them.

can you show me an instance where thay can be useful ??

best regards,
Alexandre Brisebois
 
Well, as far as I know (or use them), anonymous methods don't solve a new
problem space. They are simply a way to do what you already could do only
simpler.

button.Click += new EventHandler(ButtonClick);


void ButtonClick(object sender, EventArgs e)
{
Save();
}


can be turned into

button.Click += delegate(object sender, EventArgs e)
{
Save();
}

As you can see, i've saved myself some typing. It's pretty situational and
almost lends itself more to certain programming styles.

I will say, one place I use anonymous methods A LOT is for generic
predicates:

return allCategories.FindAll(
delegate(Category category)
{
return _categories.Contains(category.CategoryId);
}



Karl
 
Thankx Karl,

tell me what do you think of the new specs for c# 3.0?

I must admit I was a bit shocked at first to see the var keyword and
the sql like syntax to retrive objects from collections.

tell me what you think.

Best Regards,
Alexandre Brisebois
 
can you show me an instance where thay can be useful ??

You have an event you want to handle, and all it does is execute one line of
code. Do you want to create a method with one line of code, or use an
anonymous method?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
 
It's too premature for me to really pass judgment.

Years ago I programmed in Perl. It was fun, but I found the language a
little awkward. Perhaps it was getting used to regular expressions (wasn't
very good with them then), but I always thought the language was a little
burdened with having regular expression built-into it.

Similarly, I'm concerned about the burden linq will put on both C# and
VB.NET. Like any tool, it's all of matter of how it's used. Bad programmers
will write bad code, good programmers will write good code. I see "var" and
I shrug like everyone else, but like I said, I'm not too familiar with it. I
really do believe it'll be up to what individual programmers do with it.

Karl
 
good point,

I started looking at it and the first thing that came to mind was, my
god, I though learning 1.0 was a steep learning curve 3.0 for new
programmers will simply hold too many concepts into one sinlge
langues... data structures designe patterns, database, data handling,
control flows .... list goes on and on...

I have an off topic question for you,

I am currently trying to inherit from Generic.IList<T>

and I am running into problems. I simply cannot have my methods public
like this...

------------
public class FuzzySet : IList<Point>
{
List<Point> set = new List<Point>();
FuzzyCompare Comparer = new FuzzyCompare();

#region IList<Point> Members

public int IList<Point>.IndexOf(Point item)
{
return set.IndexOf(item);
}

public void IList<Point>.Insert(int index, Point item)
{
set.Inset(index, item);
}
//... ommited code for example

------------

how do I get my members public? do I have to code a second set of equal
methods to be public and have these as protected???

best regards,
Alexandre Brisebois
 
Should I create my own Generic ADT instead of implementing this
Interface ?
 
Why not simply inherit List<point> instead of the interface? seems like it
would make your life A LOT easier.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top