是“int”吗?原始类型还是对象? [英] Is "int" a primitive type or an object??

查看:91
本文介绍了是“int”吗?原始类型还是对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否int是原始类型还是对象?


例如,以下两种方法产生相同的结果。

I want to know if "int" is a primitive type, or an object?

For example, the following two approaches yield the same result.

int t1 = int。解析(TextBox2.Text); //方法1
int t2 = System.Int32.Parse(TextBox2.Text); //方法2
int t1 = int.Parse(TextBox2.Text); //method 1
int t2 = System.Int32.Parse(TextBox2.Text); //method 2




人们说int是System.Int32的C#别名。如果是这种情况,

我们可以说int是一个对象??


因为方法应该在对象上操作,而不是在原始类型上操作。


object1.method(...); //有意义

primitiveType.method(); //没有意义吗?


请指教。谢谢!



And people said "int" is a C# alias for "System.Int32". If this is the case,
can we say "int" is an object??

Because methods should operate on object, not on primitive type.

object1.method(...); //make sense
primitiveType.method(); //doesn''t make sense ??

Please advise. Thanks!

推荐答案

Int是一种主要类型。

但是,运行时可以装箱。一个灵长类型因此它可以作为一个对象操作,

从而保持一切可以被视为具有

对象基类型的范例。

请注意,值类型确实可以有方法,静态方法(例如

Parse)不需要实例句柄,因此不需要装箱

特殊案例。查找值类型和拳击在MSDN中获取更多

信息。

因此,简而言之,int(在C#中实际上只是int32)不是一个对象,但它是

可以视为一个对象。


-Rob Teixeira [MVP]


" Matt" <毫安******* @ hotmail.com>在留言中写道

news:eu ************** @ TK2MSFTNGP10.phx.gbl ...
Int is a primative type.
However, the runtime can "box" a primative so it can operate as an Object,
thereby keeping the paradigm that everything can be treated as having the
object base type.
Note that value types can indeed have methods, and static methods (like
Parse) don''t require an instance handle, so boxing isn''t required in that
particular case. Look up "value type" and "boxing" in the MSDN to get more
info.
So, in short, int (which is really just int32 in C#) isn''t an object, but it
can be treated as an object.

-Rob Teixeira [MVP]

"Matt" <ma*******@hotmail.com> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
我想知道是否" INT"是原始类型还是对象?

例如,以下两种方法产生相同的结果。
I want to know if "int" is a primitive type, or an object?

For example, the following two approaches yield the same result.
int t1 = int.Parse(TextBox2。文本); //方法1
int t2 = System.Int32.Parse(TextBox2.Text); //方法2
人们说int是System.Int32的C#别名。如果这是
int t1 = int.Parse(TextBox2.Text); //method 1
int t2 = System.Int32.Parse(TextBox2.Text); //method 2
And people said "int" is a C# alias for "System.Int32". If this is the



的情况,我们可以说int吗?是一个对象??

因为方法应该在对象上操作,而不是在原始类型上操作。

object1.method(...); //有意义
primitiveType.method(); //没有意义吗?

请指教。谢谢!


case, can we say "int" is an object??

Because methods should operate on object, not on primitive type.

object1.method(...); //make sense
primitiveType.method(); //doesn''t make sense ??

Please advise. Thanks!



2004年1月3日星期六18:38:22 -0800文章

< eu**************@TK2MSFTNGP10.phx.gbl>在

microsoft.public.dotnet.languages.csharp," Matt"

< ma ******* @ hotmail.com>写道:
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<eu**************@TK2MSFTNGP10.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Matt"
<ma*******@hotmail.com> wrote:
我想知道是否int是原始类型还是对象?

例如,以下两种方法产生相同的结果。
I want to know if "int" is a primitive type, or an object?

For example, the following two approaches yield the same result.
int t1 = int.Parse(TextBox2。文本); //方法1
int t2 = System.Int32.Parse(TextBox2.Text); //方法2
int t1 = int.Parse(TextBox2.Text); //method 1
int t2 = System.Int32.Parse(TextBox2.Text); //method 2



人们说int是System.Int32的C#别名。如果是这种情况,我们可以说int是一个对象??

因为方法应该在对象上操作,而不是在原始类型上操作。

object1.method(...); //有意义
primitiveType.method(); //没有意义吗?

请指教。谢谢!



And people said "int" is a C# alias for "System.Int32". If this is the case,
can we say "int" is an object??

Because methods should operate on object, not on primitive type.

object1.method(...); //make sense
primitiveType.method(); //doesn''t make sense ??

Please advise. Thanks!




C#编译器将对''int''的所有引用转换为''System.Int32''。

因此任何使用''int''与使用System.Int32完全相同 - 没有

的差异。为什么要使用int,那么,除了少一点之外打字?在64位系统上,它将转换为''System.Int64'' - 即

使用适合它运行的机器的大小。


在C#中(通常在IL中)没有原始类型这样的东西。

一切都是类或对象。有两种类型 -

值类型和引用类型。值类型就像原始类型

- 反正比引用类型更多 - 但要记住的重要事项是,从某种意义上说,没有''原始'类型通常使用




int(即System.Int32)不是对象。这是一堂课。作为一个

类它可以 - 事实上 - 有静态方法,这就是

..Parse(..)是。


再一次,忘记原始类型!他们不存在!


-

西蒙

雪儿湾网上的simon dot smith

失眠是您在UseNet上阅读的东西的一小部分费用



The C# compiler translates all references to ''int'' to ''System.Int32''.
Thus any use of ''int'' is exactly the same as using System.Int32 - no
difference whatsoever. Why use int, then, apart from a bit less
typing? On 64-bit systems it will translate to ''System.Int64'' - i.e.
use the appropriate size for the machine it''s running on.

In C# (in IL generally) there is no such thing as a ''primitive type''.
Everything is a class or an object. There are two types of classes -
Value types and Reference types. Value types are like primitive types
- more so than reference types anyway - but the importasnt thing to
remember is that there are NO ''primitive'' types in the sense that that
is usually used.

int (that is, System.Int32) is not an object. It is a class. As a
class it can - and in fact does - have static methods which is what
..Parse(..) is.

Once again, forget primitive types! They don''t exist!

--
Simon
simon dot smith at snowvalley dot com
"Insomnia is a small price to pay for the stuff you read on UseNet"


Simon Smith< si ********** ******@snowvalley.com>写道:
Simon Smith <si****************@snowvalley.com> wrote:
C#编译器将对''int''的所有引用转换为''System.Int32''。
因此,''int''的任何使用与使用完全相同System.Int32 - 没有任何区别。为什么要使用int,那么,除了少一点点打字?在64位系统上,它将转换为''System.Int64'' - 即
使用适合其运行的机器的大小。
The C# compiler translates all references to ''int'' to ''System.Int32''.
Thus any use of ''int'' is exactly the same as using System.Int32 - no
difference whatsoever. Why use int, then, apart from a bit less
typing? On 64-bit systems it will translate to ''System.Int64'' - i.e.
use the appropriate size for the machine it''s running on.




不,它不会。在C#中,int *总是*定义为Int32的简写,

谢天谢地。


-

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

如果回复小组,请不要给我发邮件



No it won''t. In C#, int is *always* defined as a shorthand for Int32,
thank goodness.

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


这篇关于是“int”吗?原始类型还是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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