2

Observing the System.String class in ILSpy I found the following indexer:

public extern char this[int index]
{
   [__DynamicallyInvokable, SecuritySafeCritical]
   [MethodImpl(MethodImplOptions.InternalCall)]
   get;
}

This indexer retieves the char from string by index and restricts its setter (making it immutable, I suppose). But how does this getter works? Where does it retieves the char from? And how does the debugger automaticaly implement this indexer getter?

chridam
  • 100,957
  • 23
  • 236
  • 235
Alex
  • 8,827
  • 3
  • 42
  • 58

1 Answers1

6

The description of MethodImplOptions.InternalCall states:

The call is internal, that is, it calls a method that is implemented within the common language runtime.

Thus, how the getter is implemented depends on the runtime you are using (.NET 2.0, .NET 4.0, ...), the implementation is not part of the library containing System.String.

If you are curious on how such a method could be implemented in C#, you can have a look at

Heinzi
  • 167,459
  • 57
  • 363
  • 519