Delegates are a powerful, functional language feature of C# that are heavily utilized but rarely implemented by most developers who are not familiar of the advantages they give you. Similar to function pointers in C/C++, delegates in C# allow you … Continue reading
‹ Showing all posts tagged as ‘Lambda’ ›
One Response to "How to Improve Code Reusability Using C# Delegates"
Leave a Reply:
One Response to "How to Improve Code Reusability Using C# Delegates"
-
Very interesting! Your use of delegates reminds me of the Strategy Pattern, but allows for much quicker and more concise implementation for “simpler” strategies because the developer doesn’t have to implement a whole interface which describes the “small” strategy and entire subclasses to implement the strategy. It’s definitely going into my repertoire of C# programming techniques.
I hope you don’t mind that I nitpick a little, and do correct me if I’m wrong, but I found your statement that “delegates in C# allow you to assign a type to a particular method signature” left me a little confused for a bit because “method signature” typically refers to the method’s name, order of parameters, and type of parameters. However, it seems the delegates assign not only the order and type of parameters but also the return type of the function, and it does not assign it a name. For me who doesn’t have much experience using delegates, it would have been much clearer to say “delegates in C# allow you to assign a method type to a particular order and type of parameters and return type”, so I wouldn’t mistakenly believe delegates restrict the method name.
Leave a Reply:
IList<T> Sorting: A Better Way
Programming forums across the web are replete with questions about how to sort an IList. Many .NET developers have discovered that, unlike List<T>, IList<T> does not define a Sort() method. Although there are many excellent answers to the question of … Continue reading
2 Responses to "IList<T> Sorting: A Better Way"
-
This code does not compile. It complaints at :
return list.OrderBy(t => t, new ComparisonComparer(comparison));
Saying:
Error No overload for method ‘OrderBy’ takes 2 arguments SorExtensions.cs
Very interesting! Your use of delegates reminds me of the Strategy Pattern, but allows for much quicker and more concise implementation for “simpler” strategies because the developer doesn’t have to implement a whole interface which describes the “small” strategy and entire subclasses to implement the strategy. It’s definitely going into my repertoire of C# programming techniques.
I hope you don’t mind that I nitpick a little, and do correct me if I’m wrong, but I found your statement that “delegates in C# allow you to assign a type to a particular method signature” left me a little confused for a bit because “method signature” typically refers to the method’s name, order of parameters, and type of parameters. However, it seems the delegates assign not only the order and type of parameters but also the return type of the function, and it does not assign it a name. For me who doesn’t have much experience using delegates, it would have been much clearer to say “delegates in C# allow you to assign a method type to a particular order and type of parameters and return type”, so I wouldn’t mistakenly believe delegates restrict the method name.