C#的Int32:m_value [英] C# Int32: m_value

查看:131
本文介绍了C#的Int32:m_value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读一些关于C#中的的Int32 结构后,我意识到, INT 的Int32 是同义的。在 的Int32 结构,有一个内部INT m_value 字段。

After reading a bit about the Int32 struct in C#, I realized that int and Int32 are synonymous. In the source code of Int32 struct, there is a internal int m_value field.

如果我的知识是正确的,我们分配到一个int变量的值存储在 m_value (对吗?)。但我的疑问是价值如何被保存在 m_value 当我们给 INT I = 7;

If my knowledge is right, the value that we assign to a int variable is stored in m_value (Am I right?). But my doubt is how the value gets stored in m_value when we give int i = 7;.

我看不出有任何隐含的运营商无论是在的Int32 结构的源代码,这样的价值可以得到存储在 m_value

I don't see any implicit operator either in the Int32 struct source code so that the value could get stored in m_value.

任何一个可以帮我这个?

Can any one help me with this?

推荐答案

INT 在C#中表示为 INT32 。 (虽然CIL可以做它的无符号的操作不进行强制转换)。

int in C# represents the same thing as int32 in CIL, which is a 4-byte primitive generally treated as a signed number. (Though CIL can do unsigned operations on it without a cast).

这是最低级的积木从中我们可以继续创建更复杂的结构之一,类

It's one of the lowest-level building blocks from which we can go on to create more complicated structures and classes.

但是作为这样,它不会在其上定义的任何方法

But as such, it doesn't have any methods defined on it.

System.Int32 同时看起来很像一个包装了 INT / INT32 键,确实提供了一些方法

System.Int32 meanwhile looks pretty much like a struct that wraps an int/int32 and does provide some methods.

让我们考虑它作为;让我们想想会是什么样的世界,而不 INT 与正在别名 System.Int32

Let's consider it as that; let's think about what it would be like in a world without int being aliased with System.Int32:

在这个假设的情况下,我们只被允许使用的方法 System.Int32 规定,如果我们把它作为一个特殊的装箱的int型,创造了 System.Int32 INT 当我们需要它,并提取 INT 再回来的时候,我们需要的。

In this hypothetical situation, we would only be allowed to use the methods System.Int32 provides if we treated it as a special "boxed int" type, creating a System.Int32 from an int when we needed it, and extracting the int back again when we needed that.

所以,不走样做(3).CompareTo (2)我们必须做的:

So, without aliasing to do (3).CompareTo(2) we would have to do:

new System.Int32{m_value = 3}.CompareTo(2)

不过,考虑到 INT <的内存中表示/ code> 4字节和 System.Int32 是相同的4个字节。如果我们没有一个强类型的系统,它禁止考虑一种类型为另一种类型,我们可以只把一个作为其他每当我们想要的。

But consider that the in-memory representation of int is 4 bytes and the in-memory representation of System.Int32 is the same 4 bytes. If we didn't have a strong type-system that barred considering one type as another type we could just treat one as the other whenever we wanted.

现在,C#不不允许我们这样做。例如。我们不能做的:

Now, C# does not allow us to do this. E.g. we can't do:

public struct MyInt32
{
  private int _value;
}
/* … */
MyInt32 = 3;

我们需要增加浇铸法,将被调用,否则C#只是拒绝工作它是这样的。

We would need to add a cast method that would be called, or else C# will just refuse to work on it like this.

CIL虽然没有这样的规定。它可以只是把一种类型的,只要它想另一个布局兼容的类型。因此,IL为(3).CompareTo(2)是:

CIL though has no such rule. It can just treat one type as another layout-compatible type whenever it wants. So the IL for (3).CompareTo(2) is:

ldc.i4.3 // Push the 32-bit integer 3 on the stack.
ldc.i4.2 // Push the 32-bit integer 2 on the stack.
call instance int32 [mscorlib]System.Int32::CompareTo(int32)



通话在刚刚结束假定 3 System.Int32 并调用它。

这打破了C#类型安全的规则,但这些规则并不是CIL的规则。 C#编译器也没有遵守所有的,它强制执行的规则。

This breaks the rules of C# type-safety, but those rules are not CIL's rules. The C# compiler also doesn't have to follow all of the rules that it enforces.

所以没有必要将任何东西放入 m_value ,我们只是说:哦,那四个字节那里,他们的 m_value 字段 System.Int32 ,并因此被奇迹般地完成。 (如果你知道C或C ++考虑,如果你有两个结构具有同等的成员投出指针的类型之一,会发生什么无效* 然后再返回到一个指针另一个,这是一个不好的做法,并IIRC不确定的,而不是保证,但低级别的代码被允许做的那些之类的东西)。

So there's no need to put anything into m_value, we just say "oh those four bytes there, they're the m_value field of a System.Int32", and so it is magically done. (If you know C or C++ consider what would happen if you had two structs with equivalent members and cast a pointer to one of those types to void* and then back to a pointer of another. It's a bad practice and IIRC undefined rather than guaranteed, but the lower-level code is allowed to do those sort of things).

是如何走样的作品;网络语言的编译器的特殊情况下,我们需要调用原始的方法做这种类型强制的C#代码本身不允许的情况下。

And that is how aliasing works; .Net languages' compilers special-case the cases where we need to call a method on a primitive to do this sort of type-coercion that C# code itself does not allow.

同样,特殊情况下的事实是值类型不能持有自己的类型的字段,并允许 System.Int32 有一个 INT 字段,但一般的struct {公共S值; } 将不会被允许的。

Likewise, it special cases the fact that a value-type cannot hold a field of its own type, and allows System.Int32 to have an int field, though generally struct S { public S value; } would not be allowed.

这篇关于C#的Int32:m_value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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