本文介绍了System.String 的内部结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用反射查看System.String的内部字段,发现三个字段:

I used reflection to look at the internal fields of System.String and I found three fields:

m_arrayLength

m_stringLength

m_firstChar

我不明白这是如何工作的.

I don't understand how this works.

m_arrayLength 是某个数组的长度.这个数组在哪里?它显然不是字符串类的成员字段.

m_arrayLength is the length of some array. Where is this array? It's apparently not a member field of the string class.

m_stringLength 是有道理的.这是字符串的长度.

m_stringLength makes sense. It's the length of the string.

m_firstChar 是字符串中的第一个字符.

m_firstChar is the first character in the string.

所以我的问题是字符串的其余字符在哪里?如果不在字符串类中,字符串的内容存储在哪里?

So my question is where are the rest of the characters for the string? Where are the contents of the string stored if not in the string class?

推荐答案

System.String 的大部分实现是在本机代码 (C/C++) 中而不是在托管代码 (C#) 中.如果您查看反编译代码,您会发现大多数有趣"或核心"方法都使用此属性进行修饰:

Much of the implementation of System.String is in native code (C/C++) and not in managed code (C#). If you take a look at the decompiled code you'll see that most of the "interesting" or "core" methods are decorated with this attribute:

[MethodImpl(MethodImplOptions.InternalCall)]

只有部分帮助器/便利 API 是用 C# 实现的.

Only some of the helper/convenience APIs are implemented in C#.

那么字符串的字符存储在哪里?这是最高机密!深入了解 CLR 的核心本机代码实现.

So where are the characters for the string stored? It's top secret! Deep down inside the CLR's core native code implementation.

这篇关于System.String 的内部结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:59