What is System.Linq for?

Asked

Viewed 1,592 times

19

I asked that question here at SOPT

What is the equivalent of PHP’s C array_map#?

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

Because when I tried to use the answer code above 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 (are you Missing a using Directive or an Assembly Reference?)

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

What would that be System.Linq? Why do I have to care namespace so that the function Select work in that arraysimple?

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

  • 1

    See if I need to answer anything else, or I need to get better to clarify more.

3 answers

17


That specifically is a namespace of . NET. See in another question. Just like in PHP, if something is inside a namespace needs to be "imported" to gain access to its members (unless you use the full name).

Obviously the library containing these types needs to be referenced in the project as well, but that’s another problem outside the language itself.

The method Select() is a generic algorithm for manipulating any type of IEnumerable (has question about this interface). Has various algorithms thus. This is part of the language of consultations with enumerable collections of data, the so-called LINQ.

To documentation shows interfaces implemented by type Array (beware because the class linked here is just a utilitarian for the type that is native in the language and does not represent the type itself, as it usually happens in other types).

The methods that are in this namespace specific are extension methods (specifically in static class Enumerable (see her source) and no. NET Core became more organized), 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 to find out what is available to the type when using an auto-completion mechanism of an IDE.

Use of the extension method:

objeto.Metodo()

Use of normal static method:

Classe.Metodo(objeto)

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I won’t go into details about extension methods because you already have a specific question goes up and we don’t need to repeat here.

It has a not very recommended technique for not having to do this in its extension methods: it creates the class that will contain these methods within the namespace which will usually have the types they extend. For example, if you put your methods in the namespace System, then it is likely that they will always be available. But there is a risk of having name collisions for free.

See more about the LINQ.

Example of use.

  • 1

    Interesting this last example about the extension of methods... I made a fiddle and it worked right https://dotnetfiddle.net/DYZDrl

  • 1

    I put the source code of the Enumerable of System.Linq. Yeah, it works with both syntaxes, as long as you put the this, without it only works as normal function.

13

System.Linq is the namespace implementing the Language Integrated Query (LINQ), i.e., the syntax of C# to iterate selectively on collections.

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

Not. Select in this case is an extension method implemented in System.Linq, and which only appears when mention is made of namespace.

Why do I have to care namespace so that the function Select work in that array simple?

In C#, we can implement extension methods, which are methods implemented outside 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 wear 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.

2

It is nothing more than methods of extension to your object. You need to know that: Using your collections (Array, Arraylist, Sortedlist, etc.) will gain more methods for iteration like Select Orderby Firstordefault, Lastordefault, Sort, Find and many others that will suit you in just about every case you need without the need to "reinvent the wheel".

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

Here is an example of the documentation from: https://msdn.microsoft.com/en-us/library/system.linq.enumerable(v=vs.100). aspx

Good studies.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.