拳击和拆箱? [英] Boxing and Unboxing ??

查看:61
本文介绍了拳击和拆箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Troelsen在C#和.NET平台中的说法

Boxing可以正式定义为将值明确转换为
类型的过程相应的引用类型。


我认为这个过程中我最大的问题是术语值类型

和引用类型。意味着与他们所说的完全不同的东西

所有其他语言的其他平台。通常,值类型是存储在内存中的实际数据本身(例如整数)和引用类型

只是该数据的地址。


似乎.NET已经使这两个术语中的至少一个意味着什么

完全不同。有人可以请我快速概述一下

条款和价值类型的含义。和参考类型。实际意味着它们的底层架构是什么?

According to Troelsen in "C# and the .NET Platform"
"Boxing can be formally defined as the process of explicitly converting a value
type into a corresponding reference type."

I think that my biggest problem with this process is that the terms "value type"
and "reference type" mean something entirely different than what they mean on
every other platform in every other language. Normally a value type is the
actual data itself stored in memory, (such as an integer) and a reference type
is simply the address of this data.

It seems that .NET has made at least one of these two terms mean something
entirely different. can someone please give me a quick overview of what the
terms "value type" and "reference type" actually mean in terms of their
underlying architecture?

推荐答案

值类型存储在堆栈中。当它们超出范围时,它会立即消失。主要是数字类型和结构。

引用类型存储在堆中。

系统感觉它是垃圾收集。对ref类型的引用通常作为指向地址的指针传递给
。价值类型传递了价值的副本。

我敢肯定在微软带领多年的人会在这里用更加明确和简洁的答案来表达
,但这基本上是如何



Bob
Value types are stored on the "Stack" and go away, as it were, immediately
when they go out of scope. Mostly numeric types and structs.
Reference types are stored on the "Heap" and are garbage collected when
the system feels like it. References to ref types are passed normally as
a pointer to the address. Value types are passed a copy of the value.
I''m sure someone with more years under their Microsoft belt will chime
in here with a more exlicit and concise answer, but this is basically how
it is.
Bob

>
根据Troelsen在C#和.NET平台中可以将Boxing正式定义为将值类型明确转换为相应引用类型的过程。
我认为这个过程中我最大的问题是术语
价值类型和参考类型。意味着与其他语言中的其他平台完全不同的东西。通常值
类型是存储在内存中的实际数据(例如整数)
而引用类型只是这些数据的地址。

似乎.NET已经使这两个术语中的至少一个意味着完全不同的东西。有人可以快速给我一个关于术语价值类型的概述吗?和参考类型。实际上
意味着他们的基础架构?

>
According to Troelsen in "C# and the .NET Platform"
"Boxing can be formally defined as the process of explicitly converting
a value type into a corresponding reference type."

I think that my biggest problem with this process is that the terms
"value type"
and "reference type" mean something entirely different than what they
mean on every other platform in every other language. Normally a value
type is the actual data itself stored in memory, (such as an integer)
and a reference type is simply the address of this data.

It seems that .NET has made at least one of these two terms mean
something entirely different. can someone please give me a quick
overview of what the terms "value type" and "reference type" actually
mean in terms of their underlying architecture?




发布者来自 http://www.ghytred.com/NewsLook/about.aspx



Posted by NewsLook (Trial Licence) from http://www.ghytred.com/NewsLook/about.aspx




" Bob Graham" < rv ************************ @ sbcglobal.netwrote in message

news:de ****** ************************@ghytred.com。 ..

"Bob Graham" <rv************************@sbcglobal.netwrote in message
news:de******************************@ghytred.com. ..

值类型存储在堆栈中。当它们超出范围时,它会立即消失。主要是数字类型和结构。

引用类型存储在堆中。

系统感觉它是垃圾收集。对ref类型的引用通常作为指向地址的指针传递给
。价值类型传递了价值的副本。

我敢肯定在微软带领多年的人会在这里用更加明确和简洁的答案来表达
,但这基本上是如何



Bob
Value types are stored on the "Stack" and go away, as it were, immediately
when they go out of scope. Mostly numeric types and structs.
Reference types are stored on the "Heap" and are garbage collected when
the system feels like it. References to ref types are passed normally as
a pointer to the address. Value types are passed a copy of the value.
I''m sure someone with more years under their Microsoft belt will chime
in here with a more exlicit and concise answer, but this is basically how
it is.
Bob



我要找的是全部形成所谓的额外步骤被称为

拳击和拆箱。在C / C ++中,将值类型转换为引用类型是一个非常简单的操作,我不认为有任何运行时步骤。

全部步骤在编译时完成。同样地,将参考类型

转换为值类型。


in C / C ++

int X = 56;

int * Y =& X;

现在X和* Y都保持56,Y是对X的引用;

What I am looking for is all of the extra steps that form what is referred to as
boxing and unboxing. In C/C++ converting a value type to a reference type is a
very simple operation and I don''t think that there are any runtime steps at all.
All the steps are done at compile time. Likewise for converting a reference type
to a value type.

in C/C++
int X = 56;
int *Y = &X;
Now both X and *Y hold 56, and Y is a reference to X;


>
>

>>
根据Troelsen在C#和.NET平台中的说法
拳击可以正式定义为明确地将值类型转换为相应的引用类型的过程。

我认为这个过程中我最大的问题是术语
值类型
和参考类型意味着与其他语言中的其他平台完全不同的东西。通常值
类型是存储在内存中的实际数据(例如整数)
而引用类型只是这些数据的地址。

似乎.NET已经使这两个术语中的至少一个意味着完全不同的东西。有人可以快速给我一个关于术语价值类型的概述吗?和参考类型。实际上
意味着他们的基础架构?

>>
According to Troelsen in "C# and the .NET Platform"
"Boxing can be formally defined as the process of explicitly converting
a value type into a corresponding reference type."

I think that my biggest problem with this process is that the terms
"value type"
and "reference type" mean something entirely different than what they
mean on every other platform in every other language. Normally a value
type is the actual data itself stored in memory, (such as an integer)
and a reference type is simply the address of this data.

It seems that .NET has made at least one of these two terms mean
something entirely different. can someone please give me a quick
overview of what the terms "value type" and "reference type" actually
mean in terms of their underlying architecture?




发布者来自NewsLook(试用许可证)来自
< a rel =nofollowhref =http://www.ghytred.com/NewsLook/about.aspxtarget =_ blank> http://www.ghytred.com/NewsLook/about.aspx



来自Troellsen的专业c#:


"鉴于.NET定义两种主要类型的类型(基于价值和

参考),您可能偶尔需要将一个类别的变量表示为另一个类别的变量。 C#提供了一个非常简单的
机制,称为装箱,将值类型转换为引用类型。

假设您创建了一个short类型的变量:

//制作一个短值类型。

短s = 25;

如果,在你的申请过程中,你希望代表这个

值类型作为引用类型,你可以框值如下:

//将值装入对象引用。

object objShort = s;

拳击可以正式定义为通过将变量

存储在System.Object中,将

a值类型显式转换为相应的引用类型的过程。当您装入一个值时,CLR在堆上分配一个新对象

并将值类型的值(在本例中为25)复制到

实例中。返回给你的是对新分配的

对象的引用。使用这种技术,.NET开发人员无需使用

的一组包装类,用于临时将堆栈数据视为堆分配的

对象。通过拆箱也允许相反的操作。拆箱

是将对象引用中保存的值转换回堆栈中相应值类型的过程。取消装箱操作通过验证接收数据类型是否与盒装类型相同来开始

,如果是,则将值复制回基于本地堆栈的变量。

例如,下面的拆箱操作成功,给定

,objShort的基础类型确实很短(你会检查

C#casting操作符在下一章详细介绍,所以现在紧紧抓住

)://将引用解包回相应的short。

short anotherShort =(短)objShort;"


由于我厌恶侵犯版权,我会在那里停下来。你可以为你的语言跳转拿起这本书来获得b
。它更多的是关于语言

并且与c / c和Java进行了大量的比较。

Bob
From Troellsen''s Professional c#:

"Given that .NET defines two major categories of types (value based and
reference based), you may occasionally need to represent a variable of
one category as a variable of the other category. C# provides a very simple
mechanism, termed boxing, to convert a value type to a reference type.
Assume that you have created a variable of type short:
// Make a short value type.
short s = 25;
If, during the course of your application, you wish to represent this
value type as a reference type, you would "box" the value as follows:
// Box the value into an object reference.
object objShort = s;
Boxing can be formally defined as the process of explicitly converting
a value type into a corresponding reference type by storing the variable
in a System.Object. When you box a value, the CLR allocates a new object
on the heap and copies the value type''s value (in this case, 25) into that
instance. What is returned to you is a reference to the newly allocated
object. Using this technique, .NET developers have no need to make use
of a set of wrapper classes used to temporarily treat stack data as heap-allocated
objects. The opposite operation is also permitted through unboxing. Unboxing
is the process of converting the value held in the object reference back
into a corresponding value type on the stack. The unboxing operation begins
by verifying that the receiving data type is equivalent to the boxed type,
and if so, it copies the value back into a local stack-based variable.
For example, the following unboxing operation works successfully, given
that the underlying type of the objShort is indeed a short (you''ll examine
the C# casting operator in detail in the next chapter, so hold tight for
now): // Unbox the reference back into a corresponding short.
short anotherShort = (short)objShort;"

I''ll stop there due to my distaste for violating copyrights. You may wan
to pick up this book for your language jump. It''s more about the language
and makes a lot of comparisons to c/c and Java.
Bob

>

" Bob Graham" < rv ************************ @ sbcglobal.net写在
消息新闻:de ********** ********************@ghytred.com。 ..
>

"Bob Graham" <rv************************@sbcglobal.netwrote in
message news:de******************************@ghytred.com. ..

>值类型存储在堆栈中。当它们超出范围时,它会立即消失。主要是数字类型和结构。
引用类型存储在堆中。并且当系统感觉像是垃圾收集时。对ref类型的引用通常作为指向地址的指针传递。价值类型传递了价值的副本。
我敢肯定,在他们的微软带下多年的人会在这里提出一个更加明确和简洁的答案,但这是
基本上是怎么回事。
Bob
>Value types are stored on the "Stack" and go away, as it were,
immediately when they go out of scope. Mostly numeric types and structs.
Reference types are stored on the "Heap" and are garbage collected
when the system feels like it. References to ref types are passed
normally as a pointer to the address. Value types are passed a copy of the value.
I''m sure someone with more years under their Microsoft belt will
chime in here with a more exlicit and concise answer, but this is
basically how it is.
Bob


我正在寻找的是形成什么是
的所有额外步骤,称为装箱和拆箱。在C / C中将值类型转换为
引用类型是一个非常简单的操作,我不认为
有任何运行时步骤。
所有步骤都已完成在编译时。同样地将
引用类型转换为值类型。

在C / C中int = X = 56;
int * Y =& X;


What I am looking for is all of the extra steps that form what is
referred to as boxing and unboxing. In C/C converting a value type to
a reference type is a very simple operation and I don''t think that
there are any runtime steps at all.
All the steps are done at compile time. Likewise for converting a
reference type to a value type.

in C/C
int X = 56;
int *Y = &X;
Now both X and *Y hold 56, and Y is a reference to X;


>>
>>

>>>
根据Troelsen在C#和.NET平台中的说法
拳击可以正式定义作为将值类型明确转换为相应引用类型的过程。

我认为这个过程中我最大的问题是术语
值类型 ;
和参考类型意味着与其他语言中的其他平台完全不同的东西。通常值
类型是存储在内存中的实际数据(例如整数)
而引用类型只是这些数据的地址。

似乎.NET已经使这两个术语中的至少一个意味着完全不同的东西。有人可以快速给我一个关于术语价值类型的概述吗?和参考类型。实际上
意味着它们的底层架构?

>>>
According to Troelsen in "C# and the .NET Platform"
"Boxing can be formally defined as the process of explicitly converting
a value type into a corresponding reference type."

I think that my biggest problem with this process is that the terms
"value type"
and "reference type" mean something entirely different than what they
mean on every other platform in every other language. Normally a value
type is the actual data itself stored in memory, (such as an integer)
and a reference type is simply the address of this data.

It seems that .NET has made at least one of these two terms mean
something entirely different. can someone please give me a quick
overview of what the terms "value type" and "reference type" actually
mean in terms of their underlying architecture?



发布者来自NewsLook(试用许可证)来自
http://www.ghytred.com/NewsLook/about.aspx





发自NewsLook(试用许可证)来自 http://www.ghytred.com/NewsLook/about.aspx



Posted by NewsLook (Trial Licence) from http://www.ghytred.com/NewsLook/about.aspx


这篇关于拳击和拆箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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