Extension methods are a feature of C# 3.0, but not related to the Framework version.
To use C# 3.0 with .Net 2.0, you will however need Visual Studio 2008, but it will compile and run in CLR 2.0 just like you were using C# 2.0.
By adding a
this keyword in the parameter list, you are telling the compiler to add an extension method to the class of the specified "this" type.
For example, to add average, you would do something like:
- public static double Average(this List<double> list)
-
{
-
double sum = 0.0;
-
foreach (double num in list)
-
sum += num;
-
return sum / list.Count;
-
}
Note that I haven't done any checks (if list.Count is zero for example). And note the
this keyword in parameter list.
To add a generic Average<T> to the generic List<T>, you will need to pass a delegate which knows how to convert the generic T to a number (double).