473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

List<T> Extension Method in C#2.0

33
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
Mar 17 '09 #1
3 6756
vekipeki
229 Expert 100+
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).
Mar 17 '09 #2
Jetean
33
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?
Mar 17 '09 #3
Jetean
33
@vekipeki
I test your code. There are errors. "Type Expected" when I point the Cursor to key word "This".

thanks
Mar 17 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<>...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
3
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a...
6
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
2
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists,...
11
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.