List<T> Extension Method in C#2.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jetean
    New Member
    • Feb 2008
    • 33

    List<T> Extension Method in C#2.0

    Hi:
    I was wondering how to use this Extension method ?

    Example double average = myGenericIntege rList.Average() ;

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


    Thanks
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    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:
    Code:
    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).

    Comment

    • Jetean
      New Member
      • Feb 2008
      • 33

      #3
      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?

      Comment

      • Jetean
        New Member
        • Feb 2008
        • 33

        #4
        Originally posted by vekipeki
        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:
        Code:
        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).
        I test your code. There are errors. "Type Expected" when I point the Cursor to key word "This".

        thanks

        Comment

        Working...