int number = new int(); [英] int number = new int();
问题描述
你好!
如果我写这个声明
int number = new int();
我不喜欢t得到编译器错误或运行时错误
但编译器如何阅读这样的语句?
例如,number将是对新int的引用堆?
// Tony
Hello!
If I write this statement
int number = new int();
I don''t get compiler error or run time error
but how will the compiler read such a statement ?
For example will number be a reference to a new int on the heap?
//Tony
推荐答案
Tony Johansson< jo ** ***************@telia.comwrote:
Tony Johansson <jo*****************@telia.comwrote:
如果我写这个声明
int number = new int();
我没有得到编译器错误或运行时错误
但编译器如何阅读这样的声明?
例如,number是对堆上新int的引用吗?
If I write this statement
int number = new int();
I don''t get compiler error or run time error
but how will the compiler read such a statement ?
For example will number be a reference to a new int on the heap?
编号以上与写作相同:
int number = 0;
>
" new"并不意味着在堆上创建对象在C#中 - 它只是暗示
调用构造函数。对于值类型,这不会创建一个新的
对象,它只是初始化一个新值。
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon.skeet
C#深度: http://csharpindepth.com
No. The above is the same as writing:
int number = 0;
"new" doesn''t imply "create object on the heap" in C# - it just implies
"call the constructor". For value types, that doesn''t create a new
object, it just initializes a new value.
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
int在C#中是Int32的快捷方式(即使是64位计算机)。
Int32是一个他们永远不会放在堆上的结构。
>
Cor
Hi,
int is in C# a shortcut for Int32 (even in 64bits computers).
Int32 is a structure they never are placed on the heap.
Cor
9月26日,11:58 * am,Cor Ligthert [MVP]" < notmyfirstn ... @ planet.nl>
写道:
On Sep 26, 11:58*am, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
int在C#中是Int32的快捷方式(即使在64位计算机中也是如此) )。
int is in C# a shortcut for Int32 (even in 64bits computers).
正确。
Correct.
Int32是一个他们永远不会放在堆上的结构。
Int32 is a structure they never are placed on the heap.
嗯,他们在堆上:
1)如果他们被装箱了
2)如果他们是另一个价值的一部分
例如:
class Foo
{
int x;
}
这里,任何Foo实例都存在于堆上,包括x变量。
Structs生活在堆栈上是一个神话。
见 http:// pobox.com/~skeet/csharp/memory.html 了解更多信息。
Jon
Well, they''re on the heap:
1) If they''re boxed
2) If they''re part of another value which is on the heap
For instance:
class Foo
{
int x;
}
Here, any instance of Foo lives on the heap, including its x variable.
"Structs live on the stack" is a myth.
See http://pobox.com/~skeet/csharp/memory.html for more info.
Jon
这篇关于int number = new int();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!