-2

What's actually going on here:

public decimal[] Coefficients;
public decimal this[int i]
{
    get { return Coefficients[i]; }
    set { Coefficients[i] = value; }
}

What does the this serve as? Is it some sort of extension to the decimal?

Lews Therin
  • 3,707
  • 2
  • 27
  • 53
  • 5
    If your class is called MathTest, then it allows you to access the internal Coefficients array by using MathTest[i] instead of MathTest.Coefficients[i]. See http://stackoverflow.com/questions/1307354/c-sharp-indexer-usage and http://stackoverflow.com/questions/2185071/real-world-use-cases-for-c-sharp-indexers – ManoDestra Jul 07 '16 at 18:52
  • 1
    Why the downvotes .. – PreqlSusSpermaOhranitel Jul 07 '16 at 20:48
  • 1
    No idea. I upvoted you. Question seems clear enough to me. Perhaps rather poorly researched, as it's a basic lanuage syntax question, but still valid IMO. – ManoDestra Jul 07 '16 at 21:33

3 Answers3

11

It's an Indexer.

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

Example from the linked MSDN:

class SampleCollection<T>
{
    // Declare an array to store the data elements.
    private T[] arr = new T[100];

    // Define the indexer, which will allow client code
    // to use [] notation on the class instance itself.
    // (See line 2 of code in Main below.)        
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

// This class shows how client code uses the indexer.
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();

        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
// Output:
// Hello, World.
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
2

It is an indexer it will be called when you use syntax like obj[1]. https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

Chamindu
  • 716
  • 1
  • 5
  • 13
2

Have you ever wondered how List<T>'s myList[i] works in c# just like an array ?

The Answer is in your question. The syntax you posted is a syntactic sugar that the compiler transforms into properties called get_Item(int index) and set_Item(int index, decimal value). It is used in List<T> for example to access the internal array used in the class and return the element at the specified index (set or get). This feature is called an Indexer.

To test that yourself, try to create a method with same signature :

public decimal get_Item(int i)
{
     return 0;
}

You'll get a compiler error :

Error CS0082: Type 'MyClass' already reserves a member called 'get_Item' with the same parameter types

Zein Makki
  • 29,485
  • 6
  • 52
  • 63