views:

96

answers:

5

They usually involve generics. But some methods with generics don't have them, and not all extension methods have them.

They've just "been there" since day one, we've all seen them; but I realized I still don't know what they mean, and I can't find the answer anywhere. Now it's really bugging me. Google just turns up results that are about XML, etc.

Is this officially documented anywhere? Thanks.

EDIT: Well that's just great. Since I just created an account to make my first Stack Overflow post, to get an answer for this burning question; I'm not allowed to post my pretty Intellisense picture, or create a new tag "angle-brackets". I love Stack Overflow, but... what a welcome!

Maybe my problem is that they aren't actually called "angle brackets"... ??

Anyway, I guess if you really want to see my beautiful screenshot you could manually go to:

Distinct><

Bump me up please so I can include it in the post, thanks. ;)

A: 

It means the method are generic, ie they are not type specific.

Good infomation on Generics: http://msdn.microsoft.com/en-us/library/512aeb7t.aspx

Adrian
+7  A: 

These methods are generic.

However, the compiler automatically infers the generic type parameter from the method call, so you don't need to use the brackets when calling the method.

For example, if you have an IEnumerable<int> myNumbers, the following four statements are equivalent:

myNumbers.Count();
myNumbers.Count<int>();
Enumerable.Count(myNumbers);
Enumerable.Count<int>(myNumbers);

In the first and third calls, the compiler infers the int parameter because myNumbers is an IEnumerable<int>.

The Count extension method is declared (in System.Core.Dll) like this:

public static int Count<TSource>(this IEnumerable<TSource> source);
SLaks
Nice explanation, thank you!
Dan Puza
+2  A: 

they are for generics. When you use the functions you don't always have to put in the because in many cases the compiler can figure it out by context.

gbogumil
+1  A: 

They are generic method signatures. I would suggest you learn up on them.

A couple interesting things to note about generics: First, they are not generic after compilation. All types are resolved and set in stone before you run your app. Second, the compiler can often figure out what you mean, so you don't have to be explicit about it. For instance, if a method signature is DoSomething<T>(t withThis), you could invoke this method like DoSomething<string>("with me") or you could omit the generic type: DoSomething("with me"). This is because the compiler looks at the types passed into the method as arguments and figures out the type parameter for the method. Awesome.

Will
Good link. That's the sort of thing I was looking for, thank you.
Dan Puza
A: 

Jon Skeet's book C# in Depth goes into generics and linq in detail and cleared a lot of this up for me - I can highly recommend it.

James Westgate