Connecting Tech Pros Worldwide Forums | Help | Site Map

List<T> Extension Method in C#2.0

Member
 
Join Date: Feb 2008
Location: M'sia
Posts: 33
#1: Mar 17 '09
Hi:
I was wondering how to use this Extension method ?

Example double average = myGenericIntegerList.Average();

This only available in Extension Method (I think with Framework3.0 or 3.5, but with C#3.0???)


Thanks

vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231
#2: Mar 17 '09

re: List<T> Extension Method in C#2.0


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:
Expand|Select|Wrap|Line Numbers
  1. public static double Average(this List<double> list)
  2. {
  3.     double sum = 0.0;
  4.     foreach (double num in list)
  5.         sum += num;
  6.     return sum / list.Count;
  7. }
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).
Member
 
Join Date: Feb 2008
Location: M'sia
Posts: 33
#3: Mar 17 '09

re: List<T> Extension Method in C#2.0


vekipeki:
I'm sorry. What I meant is I saw the Extension method, but in Framework 3.0 or 3.5. (you can check at MSDN under List<T> Extended Method).

My Question : Can I use Framework 3.0 or Framework 3.5 with C#2.0? Or I have to upgrade to C#3.0 in order to use the Extension?
Member
 
Join Date: Feb 2008
Location: M'sia
Posts: 33
#4: Mar 17 '09

re: List<T> Extension Method in C#2.0


Quote:

Originally Posted by vekipeki View Post

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:

Expand|Select|Wrap|Line Numbers
  1. public static double Average(this List<double> list)
  2. {
  3.     double sum = 0.0;
  4.     foreach (double num in list)
  5.         sum += num;
  6.     return sum / list.Count;
  7. }
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).

I test your code. There are errors. "Type Expected" when I point the Cursor to key word "This".

thanks
Reply