c# - 字符串索引器如何工作?

标签 c#

观察 ILSpy 中的 System.String 类,我发现了以下索引器:

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

该索引器通过索引从字符串中检索字符并限制其 setter (我认为使其不可变)。但这个 getter 是如何工作的呢? 它从哪里获取字符?调试器如何自动实现这个索引器 getter?

最佳答案

description MethodImplOptions.InternalCall 状态:

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

因此,getter 的实现方式取决于您使用的运行时(.NET 2.0、.NET 4.0,...),该实现不是包含 System.String 的库的一部分.

如果您对如何在 C# 中实现这样的方法感到好奇,可以查看

关于c# - 字符串索引器如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993180/

相关文章:

c# - Linux 无法读取 BitConverter.GetBytes() 编码的整数

c# - Asmx Web 服务有时不会响应

c# - 不可修改的变量

c# - 参数集合中已存在名为 'p__linq__0' 的参数。参数名称在参数集合中必须是唯一的

c# - 从包含一系列基于文件名进行版本控制的文件的文件夹中查找文件

c# - 如何实例化对象并将其传递给 "using" block 声明中的方法

c# - 使用 C#/VBScript 获取 mdb 文件 (msaccess) 中的所有源代码

c# - 关于流的一些事

C# 将代码添加到 FormClosing 事件会禁用 MdiParent 上的 X

c# - 在 devexpress 网格的页脚中显示总行数