C#基础知识第3部分:ReferenceEquals问题 [英] C# Fundamentals Part 3: ReferenceEquals question

查看:55
本文介绍了C#基础知识第3部分:ReferenceEquals问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好NG!


在我的书中,我有以下代码简单,我测试了它:


公共类基地:ICloneable

{

public int Age;

public string Name;


public Base(string myname)

{

Name = myname;

}


public Object Clone()

{

返回MemberwiseClone();

}


public static void Main(String [] argv)

{

基础myTest =新基地(乔安娜);

myTest.Age = 36;

Base clone1 =(Base)myTest.Clone();

Base clone2 = myTest;


bool myBTest = false;

myBTest = Object.ReferenceEquals(myTest.Name,clone1.Name);

//预订错误

//我的测试返回true


myBTest = Object.ReferenceEquals(myTest.Age,clone1.Age);

//预订真实

//我的测试返回false


myTest.Name =" Julie& ;;

myBTest = Object.ReferenceEquals(myTest.Name,clone1.Name);

//书籍说错误

//我的测试返回false

myBTest = Object.ReferenceEquals(myTest.Name,clone2.Name);

//预订saya true

//我的测试返回true

}

}


所以我有点困惑!

使用MemberwiseClone方法我做一个Shallow副本。这意味着所有

值类型都将被复制,而引用类型只是变量中的指针

。或者我是否明白这个错误?


案例1:

字符串是一种引用类型,所以对我来说很明显,为什么它会以

true。指针指向相同的字符串。或者不是?


案例2:

为什么这会返回错误?我猜是因为需要一个整数的拳击



案例3:

这是否会返回错误,因为 ;朱莉"可以写成新的

String(Julie); ?


案例4:

很明显,因为它们是两个变量,它们指向相同的

类的实例基地。


感谢您的帮助!

问候

Marcel

Hi NG!

In my book I have the following code simple and I tested it:

public class Base : ICloneable
{
public int Age;
public string Name;

public Base(string myname)
{
Name = myname;
}

public Object Clone()
{
return MemberwiseClone();
}

public static void Main(String[] argv)
{
Base myTest = new Base("Joanna");
myTest.Age = 36;
Base clone1 = (Base) myTest.Clone();
Base clone2 = myTest;

bool myBTest = false;
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns true

myBTest = Object.ReferenceEquals(myTest.Age, clone1.Age);
//Book says true
//My Test returns false

myTest.Name = "Julie";
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns false

myBTest = Object.ReferenceEquals(myTest.Name, clone2.Name);
//Book saya true
//my Test returns true
}
}

So I''m a little confused!
With the method MemberwiseClone I do a Shallow copy. That means all
value type would be copied and of the reference types just the pointer
in the variable. Or do i understand this wrong ?

Case 1:
A string is a reference type so it is clear for me, why it results as
true. The pointers points to the same string. Or not ?

Case 2:
Why does this return false ? I guess because a boxing of the to integers
is needed ?

Case 3:
Does this return wrong, because "Julie" could be written as new
String("Julie"); ?

Case 4:
It is clear, because they are two variable which points to the same
instance of the class Base.

Thanks for help!
Regards
Marcel

推荐答案

如果你这样做:


基础clone1 = myTest

基础clone2 =(基础)myTest。克隆();


相反,它可以正常工作:-)

" Marcel Hug" <毫安********** @ ch.abb.com> schrieb im Newsbeitrag

新闻:eQ **************** @ TK2MSFTNGP11.phx.gbl ...
if you''d do:

Base clone1 = myTest
Base clone2 = (Base) myTest.Clone();

instead, it would work correctly as it should :-)
"Marcel Hug" <ma**********@ch.abb.com> schrieb im Newsbeitrag
news:eQ****************@TK2MSFTNGP11.phx.gbl...
嗨NG!

在我的书中,我有以下代码简单,我测试了它:

公共类基地:ICloneable
{
public int Age;
public string Name;

public Base(string myname)
{
Name = myname;
}

公共对象克隆( )
{
返回MemberwiseClone();
}
公共静态void Main(String [] argv)
{
基础myTest = new Base(Joanna);
myTest.Age = 36;
基本clone1 =(Base)myTest.Clone();
基本clone2 = myTest;

bool myBTest = false;
myBTest = Object.ReferenceEquals(myTest.Name,clone1.Name);
//书说错误
//我的测试返回true

myBTest = Object.ReferenceEquals(myTest.Age,clone1.Age);
//书说真的
//我的测试返回false

myTest.Name =" ; Julie" ;;
myBTest = Object.Re ferenceEquals(myTest.Name,clone1.Name);
//书说错误
//我的测试返回false

myBTest = Object.ReferenceEquals(myTest.Name,clone2。姓名);
//预订saya true
//我的测试返回true
}
}

所以我有点困惑!使用MemberwiseClone方法我做一个Shallow副本。这意味着所有值的类型都将被复制,而引用类型只是
变量中的指针。或者我是否明白这个错误?

案例1:
字符串是一种引用类型,因此对我来说很清楚,为什么它的结果为真。指针指向相同的字符串。或者没有?

案例2:
为什么这会返回错误?我猜是因为需要对整数进行装箱吗?

情况3:
这是否有问题,因为Julie可以写成新的
String(Julie);案例4:
很明显,因为它们是两个变量,它们指向类Base的相同
实例。

感谢帮助!
问候
Marcel
Hi NG!

In my book I have the following code simple and I tested it:

public class Base : ICloneable
{
public int Age;
public string Name;

public Base(string myname)
{
Name = myname;
}

public Object Clone()
{
return MemberwiseClone();
}

public static void Main(String[] argv)
{
Base myTest = new Base("Joanna");
myTest.Age = 36;
Base clone1 = (Base) myTest.Clone();
Base clone2 = myTest;

bool myBTest = false;
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns true

myBTest = Object.ReferenceEquals(myTest.Age, clone1.Age);
//Book says true
//My Test returns false

myTest.Name = "Julie";
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns false

myBTest = Object.ReferenceEquals(myTest.Name, clone2.Name);
//Book saya true
//my Test returns true
}
}

So I''m a little confused!
With the method MemberwiseClone I do a Shallow copy. That means all value
type would be copied and of the reference types just the pointer in the
variable. Or do i understand this wrong ?

Case 1:
A string is a reference type so it is clear for me, why it results as
true. The pointers points to the same string. Or not ?

Case 2:
Why does this return false ? I guess because a boxing of the to integers
is needed ?

Case 3:
Does this return wrong, because "Julie" could be written as new
String("Julie"); ?

Case 4:
It is clear, because they are two variable which points to the same
instance of the class Base.

Thanks for help!
Regards
Marcel



Marcel,


寻找我的答案inline 。
Marcel,

Look for my answers inline.
在我的书中,我有以下代码简单,我测试了它:

公共类基地:ICloneable
{
公众int年龄;
公共字符串名称;

公共基地(字符串myname)
{
姓名= myname;
}
public Object Clone()
{
返回MemberwiseClone();
}
公共静态void Main(String [] argv)
{
Base myTest = new Base(Joanna);
myTest.Age = 36;
基本clone1 =(Base)myTest.Clone();
基本clone2 = myTest;

bool myBTest = false;
myBTest = Object.ReferenceEquals(myTest.Name,clone1.Name);
//书说错误
//我的测试返回true


预订说* true *因为它是浅色的副本

myBTest = Object.ReferenceEquals(myTest.Age,clone1.Age);
//书说真的
//我的测试返回false


书说假 - 拳击发生。拳击后你不能得到同样的物品

即使它们具有相同的价值。

myTest.Name =" Julie" ;;
myBTest = Object.ReferenceEquals(myTest.Name,clone1.Name);
//书说错误
//我的测试返回false


正确


myBTest = Object.ReferenceEquals(myTest.Name,clone2.Name);
//预订saya true
//我的测试返回true


正确的myTest和clone2是同一个对象。 }


所以我有点困惑!
使用MemberwiseClone方法我做一个浅的副本。这意味着所有值的类型都将被复制,而引用类型只是
变量中的指针。或者我明白这个错了吗?


这是正确的。

案例1:
一个字符串是一个引用类型,所以对我来说很清楚,为什么它会像
是的。指针指向相同的字符串。或不 ?


是的,你做得对;他们指向(引用)同一个对象。

案例2:
为什么这会返回错误?我猜是因为需要一个整数的拳击



是的,ReferenceEquals接受对象,因此所有值类型都将被加框。它没有关系价值类型的价值,它们总是会被装箱。

堆中的不同对象。
案例3:
这是否有问题,因为Julie可以写成新的
String(Julie); ?


现在是Julie和Joanna是不同的对象。
案例4:
很明显,因为它们是两个指向相同的变量
类Base的实例。
In my book I have the following code simple and I tested it:

public class Base : ICloneable
{
public int Age;
public string Name;

public Base(string myname)
{
Name = myname;
}

public Object Clone()
{
return MemberwiseClone();
}

public static void Main(String[] argv)
{
Base myTest = new Base("Joanna");
myTest.Age = 36;
Base clone1 = (Base) myTest.Clone();
Base clone2 = myTest;

bool myBTest = false;
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns true
Book says *true* because it is a shallow copy

myBTest = Object.ReferenceEquals(myTest.Age, clone1.Age);
//Book says true
//My Test returns false
Book says false - boxing happens. You can''t get the same object after boxing
even if they have the same value.

myTest.Name = "Julie";
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns false
Correct

myBTest = Object.ReferenceEquals(myTest.Name, clone2.Name);
//Book saya true
//my Test returns true
Correct myTest and clone2 are the same object. }
}

So I''m a little confused!
With the method MemberwiseClone I do a Shallow copy. That means all value
type would be copied and of the reference types just the pointer in the
variable. Or do i understand this wrong ?
This is correct.
Case 1:
A string is a reference type so it is clear for me, why it results as
true. The pointers points to the same string. Or not ?
Yes, you got it right; they point (reference) the same object.

Case 2:
Why does this return false ? I guess because a boxing of the to integers
is needed ?
Yes, ReferenceEquals accept objects, so all value types will be boxed. It
doesn''t matter the value of the value type they always will be boxed as
different objects in the heap.
Case 3:
Does this return wrong, because "Julie" could be written as new
String("Julie"); ?
Yes now Julie and Joanna are different objects.
Case 4:
It is clear, because they are two variable which points to the same
instance of the class Base.




是的,这是堆中的同一个对象。


Marcel,你说得对结果。我不知道,你是哪一本书?b $ b参考,但似乎它/它们不是一个好的


-

HTH

Stoitcho Goutsev(100)



Yes, this is the same object in the heap.

Marcel, you get correct results. I don''t know, which book(s) you are
refering to, but is seems it/they are not a good one(s)

--
HTH
Stoitcho Goutsev (100)


>如果你这样做:
> if you''d do:

基础clone1 = myTest
基础clone2 =(基础)myTest.Clone();

相反,它会正常工作,因为它应该: - )

Base clone1 = myTest
Base clone2 = (Base) myTest.Clone();

instead, it would work correctly as it should :-)




不完全正确。线路


myBTest = Object.ReferenceEquals(myTest.Age,

clone1.Age);


can永远不会回归真实,正如Stoitcho指出的那样。即使你打电话:


myBTest = Object.ReferenceEquals(5,5);


它将返回false,因为两个值必须是盒装以便将
传递给ReferenceEquals,因此它们将在堆上显示为两个单独的

对象,具有两个不同的地址。所以,书中至少有一个错误。 :-)



Not entirely true. The line

myBTest = Object.ReferenceEquals(myTest.Age,
clone1.Age);

can never return true, as Stoitcho pointed out. Even if you call:

myBTest = Object.ReferenceEquals(5, 5);

it will return false, because both values must be boxed in order to
pass them to ReferenceEquals, and so they will appear as two separate
objects on the heap, with two different addresses. So, there''s at least
one mistake in the book. :-)


这篇关于C#基础知识第3部分:ReferenceEquals问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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