基本类型:对象与否? [英] Basic types: objects or not?

查看:71
本文介绍了基本类型:对象与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否基本类型(int,long,...)objetcs?


我在C#中读到所有对象,包括从Object派生的基本类型

等级。然后在msdn文档中说,拳击转换了

对象中的基本类型。但如果它们是对象,为什么需要这种转换呢?是不是像对象这样的b $ b对象(基本类型)?

Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from Object
class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why it′s need this conversion? Aren′t
objects (basic types) like Java?

推荐答案

.Net中的原始类型等as int,float等是系统对象的别名

,如System.Int32。


在.Net中有两种主要类型的值类型和引用类型。

值类型是一种类型,其中实际值存储在变量所指向的内存区域内:


ie

int a = 3

表示a是一个32位空间的内存位置的别名

包含值3.值类型存储在堆栈中(

类型的内存区域之一)分配给进程),当你进行函数调用并返回指向

时,堆栈还存储

函数参数允许程序知道它需要跳回到哪里在函数之后

调用。


你实际上会看到对于int类型的变量,它有别名

系统.Int32,派生自System.ValueType,派生自

System.Object。


堆栈并不总是适用于某些变量,因为它是

不是非常持久,它总是在变化,其上的项目范围仅为过程调用树的最后一个
,因此需要另一种类型的变量。


另一种主要类型是参考类型。这些对象在堆栈上不存在
但在托管堆中是一个内存区域,允许存储变量

用于整个程序执行。生活在堆中的物品

可以是任何尺寸。这次当你有一个指向一个对象的变量时,变量不会直接指向该对象,但它包含了堆上对象的内存

地址




MyObject x - >包含 - > 32位数字指向 - >地址x




您将看到该值类型不能为空(因为它们没有指向

内存位置)它们必须有一个值,因为它们只是存储数据。所以

int x = null无效,而int x = -1则是。

拼图的最后一块是装箱/拆箱,拳击过程是

将值类型转换为引用类型,以便将其视为对象,

拆箱是相反的过程。我们可能想要这样做的一个简单原因是:


int i = 3;

i.ToString() ;


在这种情况下,int是一个值类型,但我们希望在代码中使用名为ToString()的方法将其视为对象

以上我正被装箱。


当你装箱时要记住一件重要的事情是

的实际价值类型是COPIED到盒装对象,当它被取消装箱时,它再次出现在对象中,因此例如:


int i = 1;

object obj =(object)i;

i = i +1;

int j =(int)obj;


print(i);

print(j);


你会看到输出2:1(可能有些人会期望2:2)


这是因为当你对象obj =(object)时,我的内部值是

在拳击时复制到obj对象中。

希望这有助于回答您的问题。

Mark。

" luis"写道:
Primitive types in .Net such as int, float etc are aliases for system objects
such as System.Int32.

In .Net there are two main kinds of types value types and reference types.
A value type is a type where the actual value is stored inside the area of
memory pointed to by the variable:

i.e.
int a = 3

means that "a" is an alias for a memory location which is a 32 bit space
containing the value 3. Value types are stored on the stack (one of the
types of areas of memory allocated to a process), the stack also stores
function parameters when you make a function call and return pointers to
allow the program to know where it needs to jump back to after a function
call.

You will actually see that for a variable of type int, it has the alias
System.Int32, which derives from System.ValueType which derives from
System.Object.

The stack is not always suitable for some kinds of variables because it is
not very persistent, it is always changing and the scope of items on it only
last for the procedure call tree, so another type of variable is needed.

The other main type are reference types. These are objects that do not live
on the stack but in the managed heap, an area of memory that allows variables
to be stored for the entire program execution. Objects living in the heap
can be of any size. This time when you have a variable pointing to an object
the variable does not point directly to the object but it contains the memory
address of the object on the heap

i.e.
MyObject x -> contains -> 32bit number which points to -> address of x on
the heap

You will see that value type can not be null (because they do not point to
memory locations) they must have a value becuase they just store data. So
int x = null is not valid whereas int x = -1 is.
The final piece of the puzzle is boxing/unboxing, the process of boxing is
to turn a value type into a reference type so it can be treated as an object,
unboxing is the opposite process. A simple reason why we may want to do this
is:

int i =3;
i.ToString();

in this case the int is a value type but we want to treat it like an object
with a method called ToString(), in the code above i is being boxed.

When you box one important thing to keep in mind is that the actual value of
the value type is COPIED into the boxed object, and when it is unboxed it is
COPIEd again out of the object, so for example:

int i =1;
object obj = (object)i;
i = i +1;
int j = (int)obj;

print(i);
print(j);

You will see the output 2 : 1 (maybe some people would expect 2 : 2)

This is because when you do object obj = (object)i the value inside is
copied into the obj object when the boxing ocurrs.
Hope this helps answer your question.
Mark.
"luis" wrote:
基本类型(int,long,...)objetcs与否?

我在C#中读到所有对象包括基本类型,派生自Object
类。然后在msdn文档中说,拳击会转换
对象中的基本类型。但如果它们是对象,为什么呢?需要这种转换吗? Aren?'t
对象(基本类型)如Java?
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from Object
class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why it?′s need this conversion? Aren?′t
objects (basic types) like Java?



谢谢。我理解这个例子和你的解释,但我怀疑

继续:基本类型(int,long)对象?


成为对象的理由:

- 我读到C#中的alll是对象(派生自Object),包括基本的

类型


不成为对象的原因:

- msdn说拳击转换对象中的基本类型

- 这些变量存储了valor,也没有引用,就像其他语言中的基本类型一样(b
)像C或java)


" luis" <关于***** @ jazzfree.com> escribióenel mensaje

news:ud ************** @ TK2MSFTNGP10.phx.gbl ...
Thanks. I understand the example and your explications but my doubt
continue: are basic types (int, long) objects?

Reasons to be objects:
- i read that alll in C# are objects (derived from Object) including basic
types

Reasons not to be objects:
- msdn says that boxing converts basic type in Object
- this variables stored the valor , nor a reference, like basic types in
others languages (like C or java)

"luis" <as*****@jazzfree.com> escribió en el mensaje
news:ud**************@TK2MSFTNGP10.phx.gbl...
是基本类型( int,long,...)objetcs与否?

我在C#中读到所有对象包括基本类型,派生自
Object类。然后在msdn文档中说,拳击会转换
对象中的基本类型。但如果它们是对象,为什么需要这种转换呢?是不是像Java这样的对象(基本类型)?
Are basic types (int, long, ...) objetcs or not?

I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic types in
objects. But if they are objects why it′s need this conversion? Aren′t
objects (basic types) like Java?



Mark R. Dawson< Ma ***** ****@discussions.microsoft.com>写道:


< snip>
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:

<snip>
值类型存储在堆栈中(内存区域的一种类型)分配给一个进程),当你进行函数调用并返回指针时,堆栈还存储
函数参数
允许程序在函数调用之后知道它需要跳回到哪里。
Value types are stored on the stack (one of the
types of areas of memory allocated to a process), the stack also stores
function parameters when you make a function call and return pointers to
allow the program to know where it needs to jump back to after a function
call.




堆栈不仅存储在堆栈中,而且这种描述在过去造成了很多人的问题。请参阅
http://www.pobox.com/ ~siget / csharp / memory.html


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该团体,请不要给我发邮件



Stacks aren''t only stored on the stack, and this description has caused
numerous people problems in the past. See
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于基本类型:对象与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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