拳击与取消装箱访问问题 [英] Boxing & UnBoxing access question

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

问题描述

大家好:


我正在使用Point类型的结构,它被传递给一个方法作为

引用类型(拳击),UpdatePoint方法改变了这个点的

的坐标:


点p =新点();

UpdatePoint(P);

..

..

..


void UpdatePoint(对象oP)

{

((Point)oP).X = 5; //错误CS0131:作业的左侧

必须

//是变量,属性或索引器

}

有人可以向我解释为什么我无法访问我盒装点?

谢谢

Ed ;;

Hi all:

I''m using a struct of type Point that is being passed on to a method as a
refrence type (boxing), the UpdatePoint method changes the coordinates of
this point as such:

Point p = new Point();
UpdatePoint( P );
..
..
..

void UpdatePoint( Object oP )
{
((Point) oP).X = 5;//error CS0131: The left-hand side of an assignment
must
// be a variable, property or indexer
}
Could someone explain to me why I can''t access my boxed Point?
Thanks
Ed;;

推荐答案

参见11.3.5拳击和取消装箱。


我要做的是创建一个新的Point,根据需要设置值并返回

新点即

Point UpdatePoint(Point oldP)

{

Point newP = newPoint();

newP.X = oldP.X + ...

...

返回newP;

}


Ed A < Ed A@discussions.microsoft.com >在留言中写道

news:89 ********************************** @ microsof t.com ...
See 11.3.5 Boxing and unboxing.

What I would do is create a new Point, set the values as needed and return
the new Point i.e.

Point UpdatePoint( Point oldP )
{
Point newP = newPoint();
newP.X = oldP.X + ...
...
return newP;
}

"Ed A" <Ed A@discussions.microsoft.com> wrote in message
news:89**********************************@microsof t.com...
大家好:

我正在使用Point类型的结构作为
参考传递给方法类型(装箱),UpdatePoint方法改变了这一点的坐标:

Point p = new Point();
UpdatePoint(P);




> UpdatePoint(Object oP)
{(/(点)oP).X = 5; //错误CS0131:作业的左侧
必须是变量,属性或索引器
}

有人可以向我解释为什么我不能访问我的盒装点?

谢谢
Ed ;;
Hi all:

I''m using a struct of type Point that is being passed on to a method as a
refrence type (boxing), the UpdatePoint method changes the coordinates of
this point as such:

Point p = new Point();
UpdatePoint( P );
.
.
.

void UpdatePoint( Object oP )
{
((Point) oP).X = 5;//error CS0131: The left-hand side of an assignment
must
// be a variable, property or indexer
}
Could someone explain to me why I can''t access my boxed Point?
Thanks
Ed;;



为什么要将UpdatePoint中的参数声明为Object 。你可以

使用ref值值类型并在函数中更改它。比如

这个:

点p =新点();

更新点(参考P);


//调用此函数后,PX应为5.

....

void UpdatePoint(ref Point pt)

{

pt.X = 5;

}


---------

Ajay Kalra
aj*******@yahoo.com


Ed A写道:
Why are you declaring the parameter in UpdatePoint as Object. You could
use ref to value type and change it in the function. Something like
this:
Point p = new Point();
UpdatePoint(ref P );

// After calling this function P.X should be 5.
....
void UpdatePoint( ref Point pt )
{
pt.X = 5;
}

---------
Ajay Kalra
aj*******@yahoo.com

Ed A wrote:
大家好:

我正在使用Point类型的结构传递给方法
作为参考类型(装箱),UpdatePoint方法更改此点的
坐标:

点p = new Point();
UpdatePoint(P );




void UpdatePoint(Object oP)
{
((Point)oP).X = 5 ; //错误CS0131:
作业的左侧必须
//是变量,属性或索引器
}

有人可以向m解释为什么我无法访问我的盒装点?

谢谢
Ed ;;
Hi all:

I''m using a struct of type Point that is being passed on to a method as a refrence type (boxing), the UpdatePoint method changes the coordinates of this point as such:

Point p = new Point();
UpdatePoint( P );
.
.
.

void UpdatePoint( Object oP )
{
((Point) oP).X = 5;//error CS0131: The left-hand side of an assignment must
// be a variable, property or indexer
}
Could someone explain to me why I can''t access my boxed Point?
Thanks
Ed;;






请记住,Point是值类型,而不是引用类型。我的意思是,你知道,因为你在谈论拳击和拆箱,但是想想

的含义。想想你的功能真正发生了什么,

逐行。


你打个电话:


UpdatePoint(p);


其中UpdatePoint接受一个Object。这包装你的Point p ...但是这是什么

这真的意味着什么?这意味着p将被_copied_到内存中的另一个
位置,放到堆上(从堆栈中,它当前是b
),并放置在一个像对象中构造(盒装)。


现在,在UpdatePoint内,你说:


((点)oP)

在这里,你是_unboxing_这一点。那是什么意思?这意味着

堆上的点(oP)将再次_copied_到一个

临时位置(在堆栈上,或许,或在寄存器中)as一点

值。现在你说:


((点)oP).X = 5;


在这里,你说你想要更改盒装点oP的

_tempoaray copy_的X坐标。但是,编译器知道

这只是堆栈或寄存器中的临时副本......

它不是一个真正的变量,你可以永远再次使用。完成

分配后,临时副本将被丢弃,并且您将保留原始值为oP的
。因此,编译器抱怨,因为

你正在做一些永远不会产生任何有意义影响的事情。


请记住:点是_value types_。他们被复制使用。是的,

他们是可变的,但是利用这种可变性来做事情最好d $
最好,因为通常很难弄清楚何时值类型是否需要复制
,当它没有被发现时,就像你发现的那样。


利用a的可变性的代码值类型很精致

代码,因而(一般来说)坏代码从维护

的角度来看。


James想出了解决这个问题的常用方法:将UpdatePoint

变成一个方法返回一个Point,并执行以下操作:


p = UpdatePoint(p);


或者,如果UpdatePoint由于某种原因必须返回一个对象,这个:


p =(点)UpdatePoint(p);


使用这种方法不会在过程中的任何一点利用

点的可变性,所以它更强大。


Ajay也提出了一个有效的想法:通过ref传递一个Point一个

对象。然而,即使在那里,我也不会这样做:


public void UpdatePoint(ref Point p)

{

pX = 5;

}


我总是喜欢这样做:


public void UpdatePoint(ref点p)

{

p =新点(5,pY);

}


同样,这也没有利用Point

字段的可变性。无论你对p做什么,都会一直有效。

Remember that Point is a value type, not a reference type. I mean, you
know that, because you''re talking about boxing and unboxing, but think
of the implications. Think of what''s really going on in your function,
line by line.

You make a call:

UpdatePoint(p);

where UpdatePoint takes an Object. This boxes your Point p... but what
does that really mean? It means that p will be _copied_ to another
place in memory, onto the heap (from the stack, where it currently
lives), and placed within an object-like construct (boxed).

Now, inside UpdatePoint, you say:

((Point)oP)

Here, you''re _unboxing_ the point. What does that mean? It means that
the Point that is on the heap (oP) will again be _copied_ to a
temporary location (on the stack, perhaps, or in a register) as a Point
value. Now you say:

((Point)oP).X = 5;

Here, you''re saying that you want to change the X coordinate of that
_tempoaray copy_ of the boxed point oP. However, the compiler knows
that this is just a temporary copy on the stack or in a register...
it''s not a real variable that you could ever use again. Once the
assignment is done, the temporary copy is thrown away and you''re left
with the original value in oP. So, the compiler complains, because
you''re doing something that could never have any meaningful effect.

Remember: Points are _value types_. They''re copied on usage. Yes,
they''re mutable, but using that mutability to do things is dodgy at
best, because it''s often difficult to figure out when a value type is
going to be copied and when it''s not, as you''ve found out.

Code that takes advantage of the mutability of a value type is delicate
code, and thus (generally speaking) "bad code" from a maintenance
standpoint.

James came up with the usual solution to this problem: turn UpdatePoint
into a method that returns a Point, and do this:

p = UpdatePoint(p);

or, if UpdatePoint must return an object for some reason, this:

p = (Point)UpdatePoint(p);

Using this method does not, at any point in the process, take advantage
of the mutability of Points, so it''s much more robust.

Ajay also came up with a valid idea: pass a Point by ref instead of an
object. However, even there, I wouldn''t do this:

public void UpdatePoint(ref Point p)
{
p.X = 5;
}

I would always prefer to do this:

public void UpdatePoint(ref Point p)
{
p = new Point(5, p.Y);
}

Again, at no point does this take advantage of the mutability of Point
fields. No matter what you do to p, it will always work.


这篇关于拳击与取消装箱访问问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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