Essentially delegates are function pointers (which is a concept used in languages like C/C++). They are also a class in the .net programming framework (System.Delegate). It is an abstract class and its sealed; meaning you cannot inherit it. Delegates are object oriented, type-safe and secure.
Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own delegated method.
Asynchronous patterns of code execution mainly employ the callback pattern. The callback function will execute once the asynchronous task completes. Developers mainly utilize this idea so that the end-users get the benefit of executing a followup routine (post asynchronous operation), but the developer need not know what that routine is or what it does. However the developer has to ensure that that routine (which is a method) has a signature that the end users specify.
Take the example of the FileStream class in the System.IO namespace. It is mainly used for I/O. It has methods like BeginRead() and BeginWrite(); they are asynchronous versions of Read() and Write() respectively. They execute on a different thread.
If you examine the prototype of the first method you would find the third argument (AsyncCallback userCallback) is actually a delegate. And, it expects a method whose return type is void and takes one argument of type IAsyncResult.
public override System.IAsyncResult BeginRead(byte[ ] array, int offset, int numBytes, System.AsyncCallback userCallback, object stateObject)
The declaration for AsyncCallback is:
public delegate void AsyncCallback(System.IAsyncResult ar)
The BeginRead() method returns a value of type IAsyncResult. If you want to wait for an asynchronous read-operation to end, you have to call EndRead() and pass the corresponding IAsyncResult value.
On a general note those classes which do some operation asynchronously have their corresponding Begin and End methods. Keep exploring to find out.
Happy programming. (and a happy new 2009). ;)
Press ctrl +E, D, It will apply the format(like indent, spacing etc) to whole current document.
FrameworkElement framework = e.OriginalSource as FrameworkElement;
ListViewItem lvi = Lstview.ItemContainerGenerator.ContainerFromItem(fe.DataContext) as ListViewItem;
Having worked for a company that developed phone fraud detection software in a previous life, I was familiar with the North American Numbering Plan described in Wikipedia. Several times I’ve had a need to create artificial phone numbers.
Below is an algorithm that will handle most if not all of these numbers.
private string GetRandomPhone()
{
// Generate a NANP compliant phone number (look in wikipedia under North American Numbering Plan)
var rnd = new Random();
// A number consists of 3 parts: NPA Nxx Station
// NPA (Numbering Plan Area code) = [2-9][0-8][0-9]
// Exclude common toll and toll-free numbers
int npa = 0;
while (npa == 0 || npa == 800 || npa == 877 || npa == 888 || npa == 900)
npa = rnd.Next(2, 9) * 100 + rnd.Next(0, 8) * 10 + rnd.Next(9);
}
// NXX (Central Office or Exchange code) = [2-9][0-9][0-9]
// Exception the second digits may not be 11
int nxx = 0;
while (nxx == 0 || nxx % 100 == 11)
nxx = rnd.Next(2, 9)*100 + rnd.Next(99);
// Station code = [0-9][0-9][0-9][0-9]
// When 555 is the NXX the number 100-199 are reserved or fictional numbers)
int station = 0;
while (station == 0 || (nxx == 555 && station > 99 && station < 200))
station = rnd.Next(9999);
const string format = "({0:000}) {1:000}-{2:0000}";
return string.Format(format, npa, nxx, station);