What is the System.Linq in C #?

17

I asked this question here in SOPT

What is the equivalent of PHP's array_map in C #?

And I was curious to know what this System.Linq is about.

Because when I tried to use the above response code without using it, I got an error:

 var arr = new int[] {1, 2, 3}.Select(x => x * 2).ToArray();
  

Compilation error (line 8, col 33): 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found you missing a using directive or an assembly reference?)

In this case, int[] by default does not have method Select ?

What would this System.Linq be? Why do I have to import this namespace so that the Select function works on this array simple?

Note : I come from PHP, where, to manipulate array , you have to use functions. So sorry if I seem to be ignorant in C #.

    
asked by anonymous 27.04.2016 / 17:48

3 answers

15

This specifically is a namespace of the .Net . See another question . Like in PHP if something is inside a namespace , it needs to be "imported" to have access to its members (unless it uses the full name).

Obviously the library containing these types needs to be referenced in the project too . But that's another problem outside the language itself.

The Select() method is a generic algorithm for manipulating any type that is a IEnumerable (you have ask about this interface ). You have Multiple Algorithms as well. This is part of the query language enumerable data collections , called LINQ .

The documentation shows the interfaces implemented by type Array (because the linked class here is just a utility for the type that is native to the language and does not represent the type itself, as is often the case with other types).

The methods that are in that particular namespace are extension methods a> (specifically in static class Enumerable ( get her source )), then they appear to be of the type itself, but are external, so they need to be explicitly referenced - through the namespace - when you want to use them.

Could be normal functions (normal static methods), but would lose the syntax uniformity and the ease of finding out what is available for the type when using a self-completion engine for an IDE.

Using the extension method:

objeto.Metodo()

Using the normal static method:

Classe.Metodo(objeto)

Example in dotNetFiddle .

I will not go into details about extension methods because you already have a specific question goes up and we do not need repeat here.

You have a not-recommended technique for not having to do this in your extension methods: it creates the class that will contain these methods within the namespace that will usually have the types they extend. For example, if you put your methods in namespace System , then they are likely to always be available. But you run the risk of having collisions of names for free.

See more about LINQ .

Usage sample .

    
27.04.2016 / 17:56
11

System.Linq is the namespace that implements Language Integrated Query (LINQ), that is, C # syntax to selectively iterate over collections.

  

In this case, int[] by default does not have method Select ?

No. Select in this case is an extension method implemented in System.Linq , which only appears when mentioning namespace .

  

Why do I have to import this namespace so that the Select function works on this simple array

In C #, we can implement extension methods, which are methods implemented outside of the original class, but which act together with the original class, extending this class.

For example:

public static class MinhasExtensions
{
    public static int[] MeuMetodo(this int[] meuarray) // O operador this diz que é uma extensão a qualquer objeto do tipo int[].
    {
        return meuarray.Select(x => x * 2).ToArray();
    }
}

So I can use it like this:

var arr = new int[] {1, 2, 3};
var outro_arr = arr.MeuMetodo(); // Isso faz a mesma coisa que o respondido na pergunta-exemplo.

I made a Fiddle .

    
27.04.2016 / 18:00
1

Nothing is more than extension methods for your object. You need to know that: Using linq your collections (Array, ArrayList, SortedList, and so on) will gain more methods for iteration like SelectBy Order, FirstOrDefault, LastOrDefault, Sort, Find and many others that will suit you in almost every case you need without the need to " reinvent the wheel ".

Remember that documentation for the .net framework itself is always a great place to get an idea of the methods and attributes that your class / object has.

Here is an example of the linq documentation: link

Good studies.

    
03.05.2016 / 20:13