# Runtime\_CompilerServices\_IndexerNameAttribute

***

**1. Simple Indexer Decoration**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**2. Conditional Indexer Decoration**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [Conditional("DEBUG")]
    [IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**3. Indexer with Additional Arguments**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index, string key] => index;
}
```

**4. Indexer with Generic Parameters**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this<T>(T index) => (int)(object)index;
}
```

**5. Indexer with Nested Type**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    public class NestedClass
    {
        [IndexerName("MyIndexer")]
        public int this[int index] => index;
    }
}
```

**6. Indexer with Template String**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("{0}_Indexer")]
    public int this[int index] => index;
}
```

**7. Indexer with IndexerState**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => index;

    public IndexerState IndexerState => null;
}
```

**8. Indexer with Custom ArgumentTransformer**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer", new string[] { "transformedArg0" })]
    public int this[int index] => index;
}
```

**9. Indexer with Custom ArgumentTransformer Factory**

```csharp
using System;
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer", argumentTransformersFactory: typeof(MyTransformerFactory))]
    public int this[int index] => index;

    public class MyTransformerFactory : ArgumentTransformerFactory
    {
        public override ArgumentTransformer CreateTransformer(string name) => null;
    }
}
```

**10. Indexer with Custom IndexerMetadata**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => index;

    public IndexerMetadata IndexerMetadata => null;
}
```

**11. Indexer with Source Position**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public int this[int index] => index;

    public string GetSourcePosition() => null;
}
```

**12. Indexer with DebuggerBrowsableAttribute**

```csharp
using System.Diagnostics;
using System.Runtime.CompilerServices;

public class MyClass
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    [IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**13. Indexer with Accessibility**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    internal[IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**14. Indexer with Protected Accessibility**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    protected[IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**15. Indexer with Internal Accessibility**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    internal[IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**16. Indexer with Private Accessibility**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    private[IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**17. Indexer with Virtual Modifier**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public virtual int this[int index] => index;
}
```

**18. Indexer with Abstract Modifier**

```csharp
using System.Runtime.CompilerServices;

public abstract class MyClass
{
    [IndexerName("MyIndexer")]
    public abstract int this[int index] { get; set; }
}
```

**19. Indexer with Override Modifier**

```csharp
using System.Runtime.CompilerServices;

public class MyClass : BaseClass
{
    [IndexerName("MyIndexer")]
    public override int this[int index] => index;
}

public class BaseClass
{
    [IndexerName("MyIndexer")]
    public virtual int this[int index] => index;
}
```

**20. Indexer with New Modifier**

```csharp
using System.Runtime.CompilerServices;

public class MyClass : BaseClass
{
    [IndexerName("MyIndexer")]
    public new int this[int index] => index;
}

public class BaseClass
{
    [IndexerName("MyIndexer")]
    public virtual int this[int index] => index;
}
```

**21. Indexer with Sealed Modifier**

```csharp
using System.Runtime.CompilerServices;

public sealed class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**22. Indexer with Static Modifier**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public static int this[int index] => index;
}
```

**23. Indexer with Extension Method**

```csharp
using System.Runtime.CompilerServices;

public static class MyExtensions
{
    [IndexerName("MyIndexer")]
    public static int this[this MyClass instance, int index] => index;
}

public class MyClass
{
}
```

**24. Indexer with Implicit Implementation**

```csharp
using System.Runtime.CompilerServices;

public interface IMyInterface
{
    [IndexerName("MyIndexer")]
    int this[int index] { get; set; }
}

public class MyClass : IMyInterface
{
    [IndexerName("MyIndexer")]
    public int this[int index] => index;
}
```

**25. Indexer with Explicit Implementation**

```csharp
using System.Runtime.CompilerServices;

public interface IMyInterface
{
    int this[int index] { get; set; }
}

public class MyClass : IMyInterface
{
    [IndexerName("MyIndexer")]
    int IMyInterface.this[int index] => index;
}
```

**26. Indexer with Property and Indexer**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => Properties[index];

    public int[] Properties { get; }
}
```

**27. Indexer with Indexer and Event**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _index;

    private int _index;

    public event EventHandler IndexChanged;
}
```

**28. Indexer with Nested Indexer**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int outerIndex]
    {
        get => this[outerIndex, 0];
        set => this[outerIndex, 0] = value;
    }

    [IndexerName("MyIndexer")]
    public int this[int outerIndex, int innerIndex] => this[outerIndex][innerIndex];
}
```

**29. Indexer with Interface Implementation**

```csharp
using System.Runtime.CompilerServices;

public interface IMyInterface
{
    [IndexerName("MyIndexer")]
    int this[int index] { get; set; }
}

public class MyClass : IMyInterface
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _array[index];

    private int[] _array;
}
```

**30. Indexer with Generic Interface Implementation**

```csharp
using System.Runtime.CompilerServices;

public interface IMyGenericInterface<T>
{
    [IndexerName("MyIndexer")]
    T this[int index] { get; set; }
}

public class MyClass<T> : IMyGenericInterface<T>
{
    [IndexerName("MyIndexer")]
    public T this[int index] => _list[index];

    private List<T> _list;
}
```

**31. Indexer with Default Value**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _array[index] ?? -1;

    private int[] _array;
}
```

**32. Indexer with Computed Value**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _array[index] * 2;

    private int[] _array;
}
```

**33. Indexer with Lambda Expression**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _array.Where(x => x == index).FirstOrDefault();

    private int[] _array;
}
```

**34. Indexer with Linq Query**

```csharp
using System.Runtime.CompilerServices;
using System.Linq;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _array.Where(x => x == index).FirstOrDefault();

    private int[] _array;
}
```

**35. Indexer with Exceptions**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index]
    {
        get
        {
            if (index < 0 || index >= _array.Length)
            {
                throw new IndexOutOfRangeException();
            }

            return _array[index];
        }
        set
        {
            if (index < 0 || index >= _array.Length)
            {
                throw new IndexOutOfRangeException();
            }

            _array[index] = value;
        }
    }

    private int[] _array;
}
```

**36. Indexer with Attributes**

```csharp
using System;
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    [Obsolete]
    public int this[int index] => index;
}
```

**37. Indexer with Span**

```csharp
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[Span<int> index] => index[0];
}
```

**38. Indexer with Memory**

```csharp
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[Memory<int> index] => index.Span[0];
}
```

**39. Indexer with ReadOnlySpan**

```csharp
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[ReadOnlySpan<int> index] => index[0];
}
```

**40. Indexer with ReadOnlyMemory**

```csharp
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[ReadOnlyMemory<int> index] => index.Span[0];
}
```

**41. Indexer with ValueTask**

```csharp
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public async ValueTask<int> this[int index] => await Task.FromResult(index);
}
```

**42. Indexer with Task**

```csharp
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public async Task<int> this[int index] => await Task.FromResult(index);
}
```

**43. Indexer with ValueTuple**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public (int, int) this[int index] => (index, index);
}
```

**44. Indexer with Reference Type**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public object this[int index] => new object();
}
```

**45. Indexer with Delegate**

```csharp
using System;
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public Func<int, int> this[int index] => x => x + index;
}
```

**46. Indexer with Extension Method and Lambda Expression**

```csharp
using System.Runtime.CompilerServices;

public static class MyExtensions
{
    [IndexerName("MyIndexer")]
    public static int this[this MyClass instance, int index] => instance.GetValue(index);
}

public class MyClass
{
    private int GetValue(int index) => index;
}
```

**47. Indexer with Disposable Wrapper**

```csharp
using System;
using System.Runtime.CompilerServices;

public class MyClass : IDisposable
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _array[index];

    private int[] _array;

    public void Dispose()
    {
        _array = null;
    }
}
```

**48. Indexer with Reflection**

```csharp
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public int this[int index] => _array[index];

    private int[] _array;

    public int GetValueFromReflection(int index)
    {
        Type type = this.GetType();
        PropertyInfo property = type.GetProperty("MyIndexer", BindingFlags.Public | BindingFlags.Instance);
        return (int)property.GetValue(this, new object[] { index });
    }
}
```

**49. Indexer with Custom Type**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public MyCustomType this[int index] => new MyCustomType(index);
}

public class MyCustomType
{
    public int Value { get; }

    public MyCustomType(int value)
    {
        Value = value;
    }
}
```

**50. Indexer with Nested Class**

```csharp
using System.Runtime.CompilerServices;

public class MyClass
{
    [IndexerName("MyIndexer")]
    public MyClass.MyNestedClass this[int index] => new MyNestedClass(index);

    public class MyNestedClass
    {
        public int Value { get; }

        public MyNestedClass(int value)
        {
            Value = value;
        }
    }
}
```
