views:

124

answers:

6

Hello. This is a question about the SYNTAX of c# and NOT about how we call/use IQueryable

Can someone please explain to me:

We have this declaration (System.Linq):

public static double Average<TSource>(this IQueryable<TSource> source,
                                      Expression<Func<TSource, int>> selector)

and to call the Average

double average = fruits.AsQueryable().Average(s => s.Length);

I understand how to call the Average and all the similar static method of IQueryable but I don’t understand the syntax of the declaration.

public static double Average<TSource>(this IQueryable<TSource> source,
                                      Expression<Func<TSource, int>> selector)

What does the <TSource> mean in Average<TSource>( and also the this IQueryable<TSource> source.

since only one parameter passes when we call it and the actual lamda expression (s => s.Length);

Thanks in advance.

+5  A: 

The <TSource> part declares the generic type parameters of the method - basically what kind of element the sequence contains. You should definitely understand generics before you get too far into LINQ.

Then,

this IQueryable<TSource> source

indicates the first parameter of the method:

  • this indicates that it's an extension method
  • IQueryable<TSource> indicates the type of the parameter
  • source is the name of the parameter

The fact that it's an extension method is probably what's confusing you. When you call

query.Average(s => s.Length)

that is converted by the compiler into

Queryable.Average(query, s => s.Length)
Jon Skeet
Here you can find more information on extension methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx Extension methods are used to 1. extend a type without needing to inherit from it2. implement methods that are valid for all types implementing a certain interfaces
Thomas
Thank a lot! You made it clear. Didn't know the syntax for extension methods!
hdiver
+1  A: 

You're declaring an extension method (that's the this keyword) that adds your method to any type implementing IQueryable<TSource> where TSource is the generic type, and remains the same throughout the expression.

The compiler can infer the generic type in this case, so you don't need to declare it when calling the method

Rowland Shaw
A: 

TSource is the generic type that you will declare. It's basically what type the s is.

derek
A: 

Average<TSource> because this is a Generic Method. The method can be run on a query or enumeration over any type (as long as a suitable selector for that type is provided).

this IQueryable<TSource> source because this is an Extension Method. Extension methods allow you to add additional methods to existing types without having to alter that type's definition. In this case Linq adds the Average method to the IQueryable interface without altering that interface.

Daniel Renshaw
Another complete answer! Thanks!
hdiver
A: 
  1. <TSource> is a generic Parameter of the method.
  2. this IQueryable<TSource> source denotes an extension method, this is syntactic sugar. In C#2.0, it would simply be a static method you'd have to call explicitly, with this, the compiler allows you to call it as if it was a member of the Type you are calling it on.
Femaref
A: 

<X> is used to make generic functions that work with different types.

X add<X>(X a, X b){return a + b;}

int a = 1;
int b = 2;
int c = add<int>(a,b);

string d = "hello ";
string e = "world";
string f = add<string>(c,d);

this is a keyword for extensions methods:

string putinsidestars(this string x){
  return "*" + x + "*";
}

string foo = "bar";
string z = foo.putinsidestars();
// z now contains *bar*
svinto