小弟请求C#帮助~~~~~~

来源:百度知道 编辑:UC知道 时间:2024/05/22 08:26:18
string str = "this is a string."; Console.WriteLine(str);
string strCopy = string.Copy(str); Console.WriteLine(strCopy);
bool testbool = (str == strCopy); Console.WriteLine(testbool);
testbool = ((object)str == (object)strCopy);
Console.WriteLine(testbool);
运行结果是:this is a string.
this is a string.
True
False
第5行比较值是否相等,第7行比较两所指的对象是否相等.为什么一个为true,一个为false,还有这里代码第5行和第7行有什么区别呢."str所指的对象"和"strCopy所指的对象"为什么不同呀~~本人比较笨,希望精解 感激不禁

运行结果是;
this is a string.
this is a string.
True
False


string str = \"this is a string.\"; Console.WriteLine(str);
输出第一个this is a string.
string strCopy = string.Copy(str); Console.WriteLine(strCopy);
输出第二个this is a string.
bool testbool = (str == strCopy); Console.WriteLine(testbool);
输出testbool值.因为两个字符串相等.所以(str == strCopy)判断为真.所以输出ture
testbool = ((object)str == (object)strCopy);
Console.WriteLine(testbool);
输出testbool值,判断str所指的对象是否和strcopy所指的对象相同.所以testbool为假.所以输出false
str和strcopy是两个不同的对象.只是内容相同.
若输出两个对象的头地址.也是不同的...

添些东西...
str和strcopy所在的区域为栈区
内存结构如下
栈区
*********|-------------------|
str******| this is a string |
*********|___________________|

*********|-------------------|
strcopy**| this is a string |
*********|___________________|

object是封箱操作.
语句执行后的内存状态

栈区
******