DynamicObject.TryGetIndex(GetIndexBinder, Object[], Object) Method

Definition

Provides the implementation for operations that get a value by index. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for indexing operations.

C#
public virtual bool TryGetIndex(System.Dynamic.GetIndexBinder binder, object[] indexes, out object result);
C#
public virtual bool TryGetIndex(System.Dynamic.GetIndexBinder binder, object[] indexes, out object? result);

Parameters

binder
GetIndexBinder

Provides information about the operation.

indexes
Object[]

The indexes that are used in the operation. For example, for the sampleObject[3] operation in C# (sampleObject(3) in Visual Basic), where sampleObject is derived from the DynamicObject class, indexes[0] is equal to 3.

result
Object

The result of the index operation.

Returns

true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)

Examples

Assume that you want to create an object in which properties can be accessed either by names such as Property0, Property1, and so on, or by index, so that, for example, sampleObject.Property0 is equivalent to sampleObject[0] in C# or sampleObject(0) in Visual Basic.

The following code example demonstrates the SampleDynamicObject class, which is derived from the DynamicObject class. The SampleDynamicObject class contains an object of the Dictionary<string, object> type (Dictionary(Of String, Object) in Visual Basic) to store the key-value pairs. SampleDynamicObject overrides the TrySetIndex and TryGetIndex methods to enable access by index. It overrides the TrySetMember and TryGetMember methods to enable access by property name.

C#
// The class derived from DynamicObject.
public class SampleDynamicObject : DynamicObject
{
    // The inner dictionary to store field names and values.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

    // Get the property value.
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        return dictionary.TryGetValue(binder.Name, out result);
    }

    // Set the property value.
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;
        return true;
    }

    // Set the property value by index.
    public override bool TrySetIndex(
        SetIndexBinder binder, object[] indexes, object value)
    {
        int index = (int)indexes[0];

        // If a corresponding property already exists, set the value.
        if (dictionary.ContainsKey("Property" + index))
            dictionary["Property" + index] = value;
        else
            // If a corresponding property does not exist, create it.
            dictionary.Add("Property" + index, value);
        return true;
    }

    // Get the property value by index.
    public override bool TryGetIndex(
        GetIndexBinder binder, object[] indexes, out object result)
    {

        int index = (int)indexes[0];
        return dictionary.TryGetValue("Property" + index, out result);
    }
}

class Program
{
    static void Test(string[] args)
    {
        // Creating a dynamic object.
        dynamic sampleObject = new SampleDynamicObject();

        // Creating Property0.
        // The TrySetMember method is called.
        sampleObject.Property0 = "Zero";

        // Getting the value by index.
        // The TryGetIndex method is called.
        Console.WriteLine(sampleObject[0]);

        // Setting the property value by index.
        // The TrySetIndex method is called.
        // (This method also creates Property1.)
        sampleObject[1] = 1;

        // Getting the Property1 value.
        // The TryGetMember method is called.
        Console.WriteLine(sampleObject.Property1);

        // The following statement produces a run-time exception
        // because there is no corresponding property.
        //Console.WriteLine(sampleObject[2]);
    }
}

// This code example produces the following output:

// Zero
// 1

Remarks

Classes derived from the DynamicObject class can override this method to specify how getting a value by index should be performed for a dynamic object. When the method is not overridden, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)

If this method is overridden, it is automatically invoked when you have an operation like sampleObject[3] in C# or sampleObject(3) in Visual Basic, where sampleObject is derived from the DynamicObject class.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0