What does this Net Core expression mean?

1

I still do not understand what this is, what they are called, and where to find documentation.

public MyClass this[int number, params object[] arguments]
{
    get
    {
        if (number < 0)
        {
            throw new ArgumentNullException();
        }

        //Do something
        return new MyClass();
    }

}

Could someone please explain u.u to me

    
asked by Mario Mixtega 02.12.2017 в 19:13
source

1 answer

3

It's not really a .net core thing, it's the c # language in general. It is an index property, it is similar to a property only that is accessed with [] and with the parameters that you establish.

An indexing property, usually used when your class handles items that require access to enumerables, for example, if you manage a list or array within your class, and you do not want to expose it as public, you can create an indexing property, with which the user who uses your class can access the elements of that enumerable in an encapsulated way.

In the example that you put, the way to use it would be:

var myclassejemplo = objeto[0, param1, param2];

In this case, object would be a type variable of the class that implements the indexer, 0 after [is the first parameter of the indexer (after this [), and param1, param2 (may be more), are the second parameter of the indexer.

    
answered by 02.12.2017 в 21:22