In .NET, what thread will Events be handled in?

After re-reading the question, I think I understand the problem now. You’ve basically got something like this: class Producer { public Producer(ExternalSource src) { src.OnData += externalSource_OnData; } private void externalSource_OnData(object sender, ExternalSourceDataEventArgs e) { // put e.Data onto the queue } } And then you’ve got a consumer thread that pulls stuff off that … Read more

Events – naming convention and style

Microsoft has actually written extensive set of naming guidelines and put it in the MSDN library. You can find the articles here: Naming Guidelines Aside from the general capitalization guidelines, here is what it has for ‘Events’ on the page Names of Type Members: ✔️ DO name events with a verb or a verb phrase. … Read more

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

Create empty C# event handlers automatically

I saw this on another post and have shamelessly stolen it and used it in much of my code ever since: public delegate void MyClickHandler(object sender, string myValue); public event MyClickHandler Click = delegate {}; // add empty delegate! //Let you do this: public void DoSomething() { Click(this, “foo”); } //Instead of this: public void … Read more

iOS: Using UIView’s ‘drawRect:’ vs. its layer’s delegate ‘drawLayer:inContext:’

How to decide which approach to use? Is there a use case for each one? Always use drawRect:, and never use a UIView as the drawing delegate for any CALayer. how come the delegate method is called if my instance is not defined to be the layer’s delegate? and what mechanism prevents drawRect from being … Read more

C#: Difference between ‘ += anEvent’ and ‘ += new EventHandler(anEvent)’

There is no difference. In your first example, the compiler will automatically infer the delegate you would like to instantiate. In the second example, you explicitly define the delegate. Delegate inference was added in C# 2.0. So for C# 1.0 projects, second example was your only option. For 2.0 projects, the first example using inference … Read more

How to change uitableview delete button text

In your controller managing the UITableView you should implement the UITableviewDelegate and return the title you want for your method inside the titleForDeleteConfirmationButtonForRowAtIndexPath method. Example: @interface CategoryAddViewController : UITableViewController @end @implementation CategoryAddViewController // … -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @”Please don’t delete me!”; } @end Leaving you off with something like that:

Using delegates in C#

The reason is the following: The way you declare the delegate it points directly to the ToString method of the static int instance. It is captured at the time of creation. As flindeberg points out in the comments below, each delegate has a target and a method to be executed on the target. In this … Read more

Simple Delegate (delegate) vs. Multicast delegates

Delegates in .NET are multicast delegates. Regardless of whether you choose to attach zero or one or several handlers to them, they are still multicast delegates. This article explains it pretty well: delegate void Del(string s); class TestClass { static void Hello(string s) { System.Console.WriteLine(” Hello, {0}!”, s); } static void Goodbye(string s) { System.Console.WriteLine(” … Read more

Creating delegates manually vs using Action/Func delegates

The advent of Action and Func family of delegates has rendered custom delegates less used, but the latter still finds uses. Advantages of custom delegates include: As others have pointed, conveys intent clearly unlike generic Action and Func (Patrik has a very good point about meaningful parameter names). You can specify ref/out parameters unlike the … Read more

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