访问C#类中的变量是否会从内存中读取整个类? [英] Does accessing a variable in C# class reads the entire class from memory?

查看:252
本文介绍了访问C#类中的变量是否会从内存中读取整个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#来说还很陌生,我有一个问题困扰了我一会儿.

I am pretty new to c# and I have one question the bothers me for a while.

当我学习C#时,我被告知一个类不应该包含很多变量,因为那样一来,读取变量(或从中调用方法)会很慢.

When I learned C# I was taught that a class shouldn't contain a lot of variables because then reading a variable (or invoking a method from it) would be slow.

有人告诉我,当我访问C#类中的变量时,它将从内存中读取整个类以读取变量数据,但这对我来说听起来很奇怪和错误.

I was told that when I access a variable in class C# it would read the entire class from memory in order to read the variable data, but it sounds weird and wrong to me.

例如,如果我有此类:

public class Test
{
    public int toAccess; // 32 bit
    private byte someValue; // 8 bit
    private short anotherValue; // 16 bit
} 

然后从main访问它:

Then accessing it from main:

public class MainClass
{
    private Test test;
    public MainClass(Test test)
    {
        this.test = test;
    }
    public static void Main(string[] args)
    {
        var main = new MainClass(new Test());
        Console.WriteLine(main.test.toAccess); // Would read all 56 bit of the class
    }
}

我的问题是:它是真的吗?访问变量时是否读取了整个类?

推荐答案

简短答案

不.

答案简短

编译器在创建中间语言代码(.NET汇编语言或IL)时会创建成员表,并且在您访问类成员时,它会在代码中指示要添加到引用中的确切偏移量(内存基址)该实例的实例).

The compiler creates members tables when it creates intermediate language code (the .NET assembly language or IL) and when you access a class member, it indicates in the code the exact offset to be added to the reference (the memory base address of the instance) of this member.

例如(以简化的形式表示),如果对象的实例的引用位于内存地址0x12345600,并且成员int Value的偏移量为0x00000010,则CLR将获得一条指令以执行以获取该区域的内容为0x12345610.

For example (in simplified shorthand), if the reference of an instance of an object is at the memory address 0x12345600, and the offset of a member int Value is 0x00000010, then the CLR will get an instruction to execute to fetch the contents of the zone in 0x12345610.

因此不需要解析内存中的整个类结构.

So it is not needed to parse the entire class structure in the memory.

好答案

这是ILSpy中Main方法的IL代码:

Here is the IL code of your Main method from ILSpy:

// Method begins at RVA 0x2e64
// Code size 30 (0x1e)
.maxstack 1
.locals init (
  [0] class ConsoleApp.Program/MainClass main
)

// (no C# code)
IL_0000: nop
// MainClass mainClass = new MainClass(new Test());
IL_0001: newobj instance void ConsoleApp.Program/Test::.ctor()
IL_0006: newobj instance void ConsoleApp.Program/MainClass::.ctor(class ConsoleApp.Program/Test)
IL_000b: stloc.0
// Console.WriteLine(mainClass.test.toAccess);
IL_000c: ldloc.0
IL_000d: ldfld class ConsoleApp.Program/Test ConsoleApp.Program/MainClass::test
IL_0012: ldfld int32 ConsoleApp.Program/Test::toAccess
IL_0017: call void [mscorlib]System.Console::WriteLine(int32)
// (no C# code)
IL_001c: nop
// }
IL_001d: ret

如您所见,WriteLine指令使用以下方法获取要写入的值:

As you can see, the WriteLine instruction get the value to write using:

IL_000d: ldfld class ConsoleApp.Program/Test ConsoleApp.Program/MainClass::test

=>在这里它加载test实例的基本内存地址(引用是一个隐藏的指针,忘记了对其进行管理)

=> Here it loads the base memory address of the test instance (a reference is an hidden pointer to forget to manage it)

IL_0012: ldfld int32 ConsoleApp.Program/Test::toAccess

=>在这里加载toAccess字段的内存地址的偏移量.

=> Here it loads the offset of the memory address of the toAccess field.

接下来,通过传递作为base + offset Int32内存区域内容的所需参数来调用WriteLine:将值压入堆栈(ldfld),被调用的方法将弹出该堆栈以获取参数(ldarg).

Next the WriteLine is called by passing the parameter needed that is the content of the base + offset Int32 memory zone: the value is pushed in the stack (ldfld) and the called method will pop this stack to get the parameter (ldarg).

在WriteLine中,您将获得以下指令来获取参数值:

In the WriteLine you will have this instruction to get the parameter value:

ldarg.1

这篇关于访问C#类中的变量是否会从内存中读取整个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆