Skip to main content

Posts

Showing posts from May, 2009

Action Delegate in .Net

Something that comes with the .Net 2.0 and above is the Action<T> delegate; this delegate can be used when ever a delegate is needed that takes in no argument or just one argument etc…Especially in GUI application it is cumbersome that you need to create delegates here and there even for simple functions, but with this new delegate defined in the System namespace you no longer need to do that, Imagine the method Show, below needs is called from a different thread then the UI thread, because you are updating UI controls created in the UI created you would need to switch back to the UI thread in order to do this (the actual reason for this?. some goggling around would do J); the code could be written like this.private void Show(int i, string j) {if (InvokeRequired) {Invoke(new Action<int,string>(Show),new object[]{i,j});return;}button1.Enabled = true;;}Notice how the Action delegate is used, as it is a generic type, the required type could be passed in. There is also an over…