How do you use Func and Action when designing applications?

They’re also handy for refactoring switch statements. Take the following (albeit simple) example: public void Move(int distance, Direction direction) { switch (direction) { case Direction.Up : Position.Y += distance; break; case Direction.Down: Position.Y -= distance; break; case Direction.Left: Position.X -= distance; break; case Direction.Right: Position.X += distance; break; } } With an Action delegate, you … Read more

Anonymous method in Invoke call

Because Invoke/BeginInvoke accepts Delegate (rather than a typed delegate), you need to tell the compiler what type of delegate to create ; MethodInvoker (2.0) or Action (3.5) are common choices (note they have the same signature); like so: control.Invoke((MethodInvoker) delegate {this.Text = “Hi”;}); If you need to pass in parameters, then “captured variables” are the … Read more

Is there a reason for C#’s reuse of the variable in a foreach?

The compiler declares the variable in a way that makes it highly prone to an error that is often difficult to find and debug, while producing no perceivable benefits. Your criticism is entirely justified. I discuss this problem in detail here: Closing over the loop variable considered harmful Is there something you can do with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)