是否有任何运行时开销为只读? [英] Is there any run-time overhead to readonly?

查看:148
本文介绍了是否有任何运行时开销为只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我一直认为只读字段已开销与它们相关的,我认为是的CLR保持跟踪不论是否只读字段已经被初始化。这里的开销是一些额外的内存使用量来跟踪状态和检查赋值时。

For some reason, I've always assumed that readonly fields have overhead associated with them, which I thought of as the CLR keeping track of whether or not a readonly field has been initialized or not. The overhead here would be some extra memory usage to keep track of the state and a check when assigning a value.

也许,我以为这是因为我不知道一个只读字段只能在构造函数中或在初始化字段声明本身并没有运行时检查,将无法保证它不被分配给多个时间中的各种方法。但现在我知道这一点,就可以很容易地通过静态的C#编译器检查,对不对?原来是这样吗?

Perhaps I assumed this because I didn't know a readonly field could only be initialized inside a constructor or within the field declaration itself and without a run-time check, you wouldn't be able to guarantee it's not being assigned to multiple times in various methods. But now I know this, it could easily be statically checked by the C# compiler, right? So is that the case?

另外一个原因是我读过的使用只读有轻微的性能影响,但他们从来没有进入这种说法,我无法找到有关此主题的信息,因此,我的问题。我不知道还有什么其他性能的影响有可能会从运行时检查是在一边。

Another reason is that I've read that the usage of readonly has a 'slight' performance impact, but they never went into this claim and I can't find information on this subject, hence my question. I don't know what other performance impact there might be aside from run-time checks.

第三个原因是,我看到了只读在编译的IL被保留为 initonly ,那么什么是这些信息要在IL如果只读无非是保证更由C#编译器领域的原因从未分配到一个构造函数或声明之外?

A third reason is that I saw that readonly is preserved in the compiled IL as initonly, so what is the reason for this information to be in the IL if readonly is nothing more than a guarantee by the C# compiler that the field is never assigned to outside of a constructor or declaration?

在另一方面,我发现了可以设置的值只读INT 通过无CLR抛出异常,这不应该是可能的,如果只读是一个运行时检查反思。

On the other hand, I've found out you can set the value of a readonly int through reflection without the CLR throwing an exception, which shouldn't be possible if readonly was a run-time check.

所以我的想法是:'readonlyness'只是一个编译时的功能,任何人都可以确认/否认这一点?而如果是,什么是被包含在IL这些信息的原因是什么?

So my guess is: the 'readonlyness' is only a compile time feature, can anyone confirm/deny this? And if it is, what is the reason for this information to be included in the IL?

推荐答案

您必须看它从相同的观点作为访问修饰符。访问修饰符的IL存在,但他们真的运行时检查? (1)我不能直接在编译时分配私有字段,(2)我可以使用反射分配。到目前为止,似乎没有运行时检查,如只读

You have to look at it from the same point of view as the access modifiers. The access modifiers exist in IL, but are they really a run-time check? (1) I can't directly assign private fields at compile-time, (2) I can assign them using reflection. So far it seems no run-time check, like readonly.

不过,让我们来看看访问修饰符。做到以下几点:

But let's examine access modifiers. Do the following:


  1. 公共C类

  2. 创建大会A.DLL创建一个组件B .EXE引用A.DLL。 B.EXE使用C类

  3. 构建两个组件。运行B.EXE作品就好。

  4. 重建A.DLL但设置C类内部。在B.EXE目录替换A.DLL。

  1. Create Assembly A.dll with public class C
  2. Create an Assembly B.exe that references A.dll. B.exe uses class C.
  3. Build the two assemblies. Running B.exe works just fine.
  4. Rebuild A.dll but set class C to internal. Replace A.dll in B.exe's directory.

现在,运行B.EXE抛出一个运行时异常。

Now, running B.exe throws a runtime exception.

访问修饰符的IL也同样存在,对不对?那么,什么是他们的目的是什么?其目的是使引用.NET程序集的其他程序集需要知道他们被允许访问的内容和他们不准进入,编译时和运行时。

Access modifiers exist in IL as well, right? So what's their purpose? The purpose is that other assemblies that reference a .Net assembly need to know what they are allowed to access and what they are not allowed to access, both compile-time AND run-time.

只读似乎有一个类似的目的IL 的。它告诉其他组件是否可以写入字段上的特定类型。然而,的只读似乎并不以有同样的运行时检查访问修饰符表现出我的样本上面。看来,只读是一个编译时检查,并在运行时不会发生。看看性能的样品在这里:只读性能VS常量

Readonly seems to have a similar purpose in IL. It tells other assemblies whether they can write to a field on a particular type. However, readonly does not seem to have that same run-time check that access modifiers exhibit in my sample above. It seems that readonly is a compile-time check and does not occur in run-time. Take a look at a sample of performance here: Read-only performance vs const.

同样,这并不意味着IL是没用的。该IL可确保在第一时间发生编译时错误。记住,当你建立你不建反代码,但组件。

Again, this doesn't mean the IL is useless. The IL makes sure that a compile-time error occurs in the first place. Remember, when you build you don't build against code, but assemblies.

这篇关于是否有任何运行时开销为只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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