具有“值类型”的运算符new [英] operator new with "value type"

查看:100
本文介绍了具有“值类型”的运算符new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,


我是开发人员来自C ++和Java ...我已经通过

有效C# (强烈推荐那些来自其他想要学习C#的b $ b语言的人),并指出价值类型。总是在堆栈上创建
,但随后显示了创建。一个w /

运算符new。我想这很有意义,但是这是真的:


MyValueType t;





MyValueType t = new MyValueType();


与对象的存储位置相同?那只是

对我来说似乎很奇怪。我想这可能是在某个地方的常见问题解答中(我希望
欢迎指向任何常见问题解答列表的任何指针),但是我没有找到明确表示的b $ b我没有花太多时间

看:) ...


-Charlie


Hello,

I''m a developer coming from C++ and Java ... I''ve going thru
"Effective C#" (which highly recommend for people coming from other
languages wanting to learn C#), and it states that "value types" are
always created on the stack, but then showed the "creation" of one w/
operator new. I guess it makes sense, but is it true that:

MyValueType t;

and

MyValueType t = new MyValueType();

are equivalent w/ regard to where the object is stored? That just
seems odd to me. I suppose this is probably in a FAQ somewhere (I
welcome any pointers to any FAQ list that answers this), but I
couldn''t find it stated obviously (tho I didn''t spend too long
looking :) ...

-Charlie

推荐答案

2007年12月7日星期五12:22:29 -0800,carnold< Ch ************ @ gmail.com

写道:
On Fri, 07 Dec 2007 12:22:29 -0800, carnold <Ch************@gmail.com
wrote:

[...]我想这是有道理的,但确实如此:


MyValueType t;





MyValueType t = new MyValueType();


相当于尊重到对象存储的位置?那只是

对我来说似乎很奇怪。
[...] I guess it makes sense, but is it true that:

MyValueType t;

and

MyValueType t = new MyValueType();

are equivalent w/ regard to where the object is stored? That just
seems odd to me.



对我而言。但是,是的,这两个数据相当于存储数据

的位置。恰好,值类型使用新值。运营商

用于初始化,即使在这种情况下没有分配实际内存。


Pete

To me too. But yes, the two are equivalent with respect to where the data
is stored. It just happens that the value types use the "new" operator
for initializing even though no actual memory is allocated in that case.

Pete


carnold< Ch ************ @ gmail.comwrote:
carnold <Ch************@gmail.comwrote:

我是开发人员来自C ++和Java ...我已经通过

有效的C# (强烈推荐那些来自其他想要学习C#的b $ b语言的人),并指出价值类型。总是在堆栈上创建

I''m a developer coming from C++ and Java ... I''ve going thru
"Effective C#" (which highly recommend for people coming from other
languages wanting to learn C#), and it states that "value types" are
always created on the stack



如果确切地说,那就错了。

请参阅 http://pobox.com/~skeet/csharp/memory。 html


然后显示创建一个w /

运算符new。我想这很有意义,但是这是真的:


MyValueType t;





MyValueType t = new MyValueType();


与对象的存储位置相同?
but then showed the "creation" of one w/
operator new. I guess it makes sense, but is it true that:

MyValueType t;

and

MyValueType t = new MyValueType();

are equivalent w/ regard to where the object is stored?



是的。变量的值是MyValueType的一个实例,它存储在变量上下文的任何位置。

Yes. The value of the variable is an instance of MyValueType, and it''s
stored wherever the context of the variable is.


那个对我来说似乎很奇怪。
That just seems odd to me.



我认为它具有完美的逻辑意义,但它*与C ++不同。


-

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

英国的世界级.NET培训: http://iterativetraining.co.uk

I think it makes perfect logical sense, but it *is* different to C++.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


周五,2007年12月7日15:57:17 - 0800,carnold< Ch ************ @ gmail.com>

写道:
On Fri, 07 Dec 2007 15:57:17 -0800, carnold <Ch************@gmail.com>
wrote:

[ ...]

这是一个很好理解的设计原则,你不应该改变熟悉的结构的含义。
[...]
It''s a well understood design principal that you shouldn''t change the
meaning of familiar constructs.



人们也很清楚,可以使用相同语法表示的行为是不同的b
只要行为与b $ b相似就足够了。这就是为什么操作员超载,甚至超载

方法,不仅在许多情况下都很有价值。


它不是一个/或,黑白问题。

It''s also well-understood that it''s okay to have behaviors that are
different represented by the same syntax, as long as the behavior is
similar enough. That''s why operator overloading, and even overloading of
methods, is not only but valuable in many cases.

It''s not an either/or, black-and-white question.


" new"在C#和Java中具有特定的含义C ++和Java都没有用C#...我保证你不是第一个认为改变那些的人意味着一个坏主意。
"new" has specific implications in
both C++ and Java that don''t hold in C# ... I guarantee you I''m not
the first one to think changing those implications a bad idea.



你不需要保证他。他知道。他听到我过去抱怨过同样的事情。

。 :)


我仍​​然希望使用不同的语法,但我确实理解

是那些说使用new的人的基础。比其他

替代品更好。我不一定同意,但我不是那个得到这个语言的人。这个问题太主观了我无法宣称

那里'这是一个绝对的事实。


Pete

You don''t need to guarantee him. He knows. He''s heard me complain about
the very same thing in the past. :)

I still wish that a different syntax had been used, but I do understand
the basis for those who say that using "new" is better than other
alternatives. I don''t necessarily agree, but I''m not the one who got to
design the language and the issue is too subjective for me to claim
there''s an absolute truth to be found.

Pete


这篇关于具有“值类型”的运算符new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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