When do we have to use delegate in C#?
-
Hi there. I'm new on C# :sigh: . I want to know when we have to use delegate in C#.:confused: Of course except Eevents. Thanks in advance.
Sorry for my English. I'm a freshman .
-
Hi there. I'm new on C# :sigh: . I want to know when we have to use delegate in C#.:confused: Of course except Eevents. Thanks in advance.
Sorry for my English. I'm a freshman .
You can use delegates for heaps of things, they are just function pointers. I used them quite often when multithreading. Events are where they seem to be most commonly used though.
Mark Brock Click here to view my blog
-
Hi there. I'm new on C# :sigh: . I want to know when we have to use delegate in C#.:confused: Of course except Eevents. Thanks in advance.
Sorry for my English. I'm a freshman .
Work @ Network integrated solutions | Flickr | A practical use of the MVC pattern citizen 340340
-
You can use delegates for heaps of things, they are just function pointers. I used them quite often when multithreading. Events are where they seem to be most commonly used though.
Mark Brock Click here to view my blog
Thanks Mark.
Sorry for my English. I'm a freshman .
-
Work @ Network integrated solutions | Flickr | A practical use of the MVC pattern citizen 340340
- u can use a delegate if u want an object to have several method with same signature (ie, a family of Methods) but with different algorithm and u can select between these method dynamically using a delegate. Something like... delegate double BinaryOperation(double a, double b) BinaryOperation binaryOperation; private double Add(double a, double b) { return a + b; } private double divide(double a, double b) { return a / b; } public static void BinaryOperationsButton_click(sender object, EventArgs e) { if(object is btnAdd) binaryOperation = new BinaryOperation(Add); else if(object is btnDivide) binaryOperation = new BinaryOperation(Divide); binayOperation.BeginInvoke(operand1,operand2,null,null); }
-
- u can use a delegate if u want an object to have several method with same signature (ie, a family of Methods) but with different algorithm and u can select between these method dynamically using a delegate. Something like... delegate double BinaryOperation(double a, double b) BinaryOperation binaryOperation; private double Add(double a, double b) { return a + b; } private double divide(double a, double b) { return a / b; } public static void BinaryOperationsButton_click(sender object, EventArgs e) { if(object is btnAdd) binaryOperation = new BinaryOperation(Add); else if(object is btnDivide) binaryOperation = new BinaryOperation(Divide); binayOperation.BeginInvoke(operand1,operand2,null,null); }
I've seen similar examples but I still can't see the point. What's wrong with this code: { if (sender.name == "btnAdd") { double result = Add(operand1, operand2); } if (sender.name == "btnDivide") { double result = divide(operand1, operand2); } } for me makes reading and understanding simpler. So, I am not using delegates, and worrying i'm missing something ;) Rob
-
I've seen similar examples but I still can't see the point. What's wrong with this code: { if (sender.name == "btnAdd") { double result = Add(operand1, operand2); } if (sender.name == "btnDivide") { double result = divide(operand1, operand2); } } for me makes reading and understanding simpler. So, I am not using delegates, and worrying i'm missing something ;) Rob
:) the previous example did not do its job even though it show the delegates asynchronous ability. Something assignable gives you the flexibility and scalability. just look at this situation; delegate void CipherInvoker(byte[] array); CipherInvoker cipher; public void ChooseCipher() { if(option == "RSACipher") cipher = new CipherInvoker(RSACipher); else if(option == "DESCipher") cipher = new CipherInvoker(DESCipher); CipherFile("blah", cipher); } public void CipherFile(string filname, CipherInvoker cipher) { // blah blah cipher(); //blah blah } lets assume that , you found out new efficient and secure way of ciphering or u want to implement ciphering with a another alogorithm; then u can implement it and u can use it without altering unnecessary places u may change like public void ChooseCipher() { if(option == "RSACipher") cipher = new CipherInvoker(RSACipher); else if(option == "DESCipher") cipher = new CipherInvoker(DESCipher); else if(option == "MyCipher") cipher = new CipherInvoker(MyCipher); CipherFile("blah" ,cipher); }