Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of the angle brackets on LINQ methods in Intellisense? (Contains<>, Count<>, Distinct<>, etc.)

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>< http://www.freeimagehosting.net/uploads/6a6c2f3268.png

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

like image 384
Dan Puza Avatar asked Apr 09 '10 13:04

Dan Puza


People also ask

What do angle brackets mean in C#?

In c#, generics are represented by using angle bracket <>. To define a class or method as generic, we need to use a type parameter as a placeholder with angle (<>) brackets. The compiler will replace all the placeholders with the specified type at compile time.

What are angle brackets in coding?

The angle bracket (< or >), which is also called an “inequality sign” for its use in mathematics, is a kind of sideways caret that can be used to include tags or pieces of code. This ASCII set of characters is common in web design and other types of coding projects.


2 Answers

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);
like image 50
SLaks Avatar answered Nov 09 '22 01:11

SLaks


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.

like image 30
Greg Bogumil Avatar answered Nov 09 '22 00:11

Greg Bogumil