C #3.0 adds many new features. where. the extension method allows us to extend the class/interface functions without modifying the original code. skillful Use of this feature. write efficient, easy to maintain, and beautiful code.
Enhance existing interfaces
C #2.0 provides many generic and container interfaces and classes. the most common example is IComparable <T>. You often need to create a new Comparable for sorting and comparison .. however, it only has one CompareTo method by default. before. we can only manually add. and it is difficult to reuse code. now. use the extension method. this interface can be easily extended.
Public static class Comparable
{
Public static bool LessThan <T> (this T left, T right) where T: IComparable <T>
{
Return left. CompareTo (right) <0;
}
Public static bool GreaterThan <T> (this T left, T right) where T: IComparable <T>