JAVA程序员向C#程序员提问 [英] Question of JAVA programmer to C# programmers

查看:116
本文介绍了JAVA程序员向C#程序员提问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,每天都用java和b $ bi wotk,但上次我对C#感兴趣。

除了一件事,一切都很顺利......
< java中的
一切都是引用(普通类型除外)所以我认为

在C#中会是一样的 - 我错了......

或者如果在C#中与java相同请告诉我;-)


我有一个ArrayList名称ls:

// Point是一个class System.Draw ....

//请原谅我的代码错误,但我是从脑子里写的;-)


ArrayList ls = new ArrayList [2];

ls [0] = new Point(0,0);

ls [1] = new Point(10,10);


点p =(点)ls [0];

pX = 20;

pY = 20;


// print p.ToString()

//((Point)ls [0])。ToString()

//请注意(p!=(Point)ls [0])= TRUE

为什么p是对象的副本Point存储为ArrayList的第一个元素

(ls [0 ])


D. oi有什么问题???


我在浏览MSDN但是我没有找到情况说明

如上所示。

解决方案

你好,


没有错。 Point是一种所谓的值类型(按值而不是通过引用存储在
内存中)。但是ArrayList只能存储从System.Object派生的任何东西,这是一个引用类型。因此,运行时某种程度上需要将值类型转换为引用类型。这种

a转换称为拳击,相反的转换称为

拆箱。


所以你遇到的是一点点CLR诀窍曾经有能力使用

值类型,需要引用类型。


在''拳击'值类型'''上搜索MSDN更多详情。


-

此致,

Dmitriy Lapshin [C#/ .NET MVP]

今天将单元测试的强大功能带到VS .NET IDE中!
http://www.x-unity.net/teststudio.aspx


" Xandau" < MC **** @ box43.pl>在留言中写道

news:ca ********** @ news2.ipartners.pl ...

你好,
我wotk每天都有java,但上次我对C#感兴趣。
一切都很好,除了一件事......

在java中一切都是参考(普通类型除外)所以我想到了/>在C#中将是相同的 - 我错了....
或者如果在C#中与java相同请告诉我;-)

我有一个ArrayList名称ls:
// Point是一个类System.Draw ....
//请原谅我代码中的错误,但我是从脑子里写的;-)

ArrayList ls = new ArrayList [2];
ls [0] = new Point(0,0);
ls [1] = new Point(10,10);

点p =(点)ls [0];
pX = 20;
pY = 20;

//打印p.ToString()
/ /((Point)ls [0])。ToString()

//请注意(p!=(Point)ls [0])= TRUE
为什么p是副本对象Point存储为ArrayList的第一个元素
(ls [0])???

我有什么问题???

我在浏览MSDN,但我没有找到上述情况的解释。



Xandau< mc **** @ box43.pl>写道:

我每天都和java一起玩,但上次我对C#感兴趣。
一切都很好,除了一件事......

在java中一切都是一个参考(普通类型除外)所以我认为在C#中将是相同的 - 我错了....
或者如果在C#中与java相同请告诉我;-)


并非Java中的所有内容都是引用 - 有值类型

(int,char,bool等)它只是你不能定义你自己的值类型。

我有一个ArrayList名称ls:
// Point是一个类System.Draw ....




否,Point是System.Drawing中的* struct *。这是一种价值类型,而不是

参考类型。


-

Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~双向飞碟

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


Xandau,


默认情况下,C#类实现引用相等,这就是你的意思。

但是对于特定的类,更有意义的是覆盖这种行为

和实现基于特定属性,该类的值相等,

与Point类的情况一样。


希望有所帮助。


问候,

Wim Hollebrandse

-
http://www.wimdows.net
http://www.wimdows.com

" Xandau" < MC **** @ box43.pl>在留言中写道

news:ca ********** @ news2.ipartners.pl ...

你好,
我wotk每天都有java,但上次我对C#感兴趣。
一切都很好,除了一件事......

在java中一切都是参考(普通类型除外)所以我想到了/>在C#中将是相同的 - 我错了....
或者如果在C#中与java相同请告诉我;-)

我有一个ArrayList名称ls:
// Point是一个类System.Draw ....
//请原谅我代码中的错误,但我是从脑子里写的;-)

ArrayList ls = new ArrayList [2];
ls [0] = new Point(0,0);
ls [1] = new Point(10,10);

点p =(点)ls [0];
pX = 20;
pY = 20;

//打印p.ToString()
/ /((Point)ls [0])。ToString()

//请注意(p!=(Point)ls [0])= TRUE
为什么p是副本对象Point存储为ArrayList的第一个元素
(ls [0])???

我有什么问题???

我在浏览MSDN,但我没有找到上面提到的情况说明。



hello all,
i wotk with java every day but last time i have interested in C#.
everything goes great except one thing...

in java everything is a reference (except plain types) so i thought
that in C# will be the same - i was wrong....
or if in C# is the same as in java please tell me ;-)

I have an ArrayList names ls :
// Point is a class System.Draw....
// Please excuse me an errors in code, but i write it from my head ;-)

ArrayList ls = new ArrayList[2];
ls[0] = new Point(0,0);
ls[1] = new Point(10,10);

Point p = (Point) ls[0];
p.X = 20;
p.Y = 20;

// print p.ToString()
// ((Point) ls[0]).ToString()
// please notice that (p!= (Point) ls[0]) = TRUE
why p is a copy of object Point stored as first element of ArrayList
(ls[0])???

Do i something wrong???

I was browsing MSDN but i haven''t found an explanation of situation
presented above.

解决方案

Hello,

There''s nothing wrong. Point is a so-called value type (which is stored in
memory by value, not by reference). But ArrayList is only capable of storing
anything derived from System.Object which is a reference type. Therefore,
the runtime somehow needs to convert the value type to a reference one. Such
a conversion is called boxing, and the opposite conversion is called
unboxing.

So what you experience is a little CLR trick used to have an ability to use
value types where a reference type is required.

Search MSDN on ''boxing "value type"'' for more details.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Xandau" <mc****@box43.pl> wrote in message
news:ca**********@news2.ipartners.pl...

hello all,
i wotk with java every day but last time i have interested in C#.
everything goes great except one thing...

in java everything is a reference (except plain types) so i thought
that in C# will be the same - i was wrong....
or if in C# is the same as in java please tell me ;-)

I have an ArrayList names ls :
// Point is a class System.Draw....
// Please excuse me an errors in code, but i write it from my head ;-)

ArrayList ls = new ArrayList[2];
ls[0] = new Point(0,0);
ls[1] = new Point(10,10);

Point p = (Point) ls[0];
p.X = 20;
p.Y = 20;

// print p.ToString()
// ((Point) ls[0]).ToString()
// please notice that (p!= (Point) ls[0]) = TRUE
why p is a copy of object Point stored as first element of ArrayList
(ls[0])???

Do i something wrong???

I was browsing MSDN but i haven''t found an explanation of situation
presented above.




Xandau <mc****@box43.pl> wrote:

i wotk with java every day but last time i have interested in C#.
everything goes great except one thing...

in java everything is a reference (except plain types) so i thought
that in C# will be the same - i was wrong....
or if in C# is the same as in java please tell me ;-)
Not everything in Java is a reference either - there are value types
(int, char, bool etc) it''s just you can''t define your own value types.
I have an ArrayList names ls :
// Point is a class System.Draw....



No, Point is a *struct* in System.Drawing. It''s a value type, not a
reference type.

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


Xandau,

By default, C# classes implement reference equality, which is what you mean.
For specific classes however, it makes more sense to override this behavior
and implement value equality on that class, based on particular properties,
as is the case with the Point class.

Hope that helps.

Regards,
Wim Hollebrandse
--
http://www.wimdows.net
http://www.wimdows.com
"Xandau" <mc****@box43.pl> wrote in message
news:ca**********@news2.ipartners.pl...

hello all,
i wotk with java every day but last time i have interested in C#.
everything goes great except one thing...

in java everything is a reference (except plain types) so i thought
that in C# will be the same - i was wrong....
or if in C# is the same as in java please tell me ;-)

I have an ArrayList names ls :
// Point is a class System.Draw....
// Please excuse me an errors in code, but i write it from my head ;-)

ArrayList ls = new ArrayList[2];
ls[0] = new Point(0,0);
ls[1] = new Point(10,10);

Point p = (Point) ls[0];
p.X = 20;
p.Y = 20;

// print p.ToString()
// ((Point) ls[0]).ToString()
// please notice that (p!= (Point) ls[0]) = TRUE
why p is a copy of object Point stored as first element of ArrayList
(ls[0])???

Do i something wrong???

I was browsing MSDN but i haven''t found an explanation of situation
presented above.



这篇关于JAVA程序员向C#程序员提问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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