首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

引用类型和值类型的有关问题,难道string不是引用类型?看看这个错那了

2012-01-08 
引用类型和值类型的问题,难道string不是引用类型?看看这个错那了?欲证明C#中引用类型,改变两个相等的引用

引用类型和值类型的问题,难道string不是引用类型?看看这个错那了?
欲证明   C#   中引用类型,改变两个相等的引用类型,则另一个也随之改变。结果发现class   可以,但string不行。为什么?

Employee   ee1   =   new   Employee();
Employee   ee2   =   ee1;
现在,因为类在   C#   中为引用类型,所以   ee1   被视为对   Employee   的引用。在前面的两行中,第一行在内存中创建   Employee   的一个实例,并设置   ee1   以引用该实例。因此,将   ee2   设置为等于   ee1   时,它包含了对内存中的类的重复引用。如果现在更改   ee2   上的属性,则   ee1   上的属性将反映这些更改,因为两者都指向内存中的相同对象,如下所示:
原文:http://msdn2.microsoft.com/zh-cn/library/ms228360(vs.80).aspx

小实验如下:
Default2.aspx.cs代码
using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;

public   partial   class   Default2   :   System.Web.UI.Page
{
        class   class1
        {
                public   int   vau   =   000;
        }
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                string   str1   =   "str1   ";
                String   str2=str1;
                str2   =   "str1   xxx ";

                Label1.Text   =   str1;
                Label2.Text   =   str2;
 

                int   i   =   10;
                int   k   =   i;
                i   =   20;
                Label3.Text   =   i.ToString();
                Label4.Text   =   k.ToString();

                class1   a   =new   class1();
                class1   b   =   a;
                b.vau   =   123;
                Label5.Text   =   a.vau.ToString();
                Label6.Text   =   b.vau.ToString();

               
        }
}


Default2.aspx的代码
<%@   Page   Language= "C# "   AutoEventWireup= "true "   CodeFile= "Default2.aspx.cs "   Inherits= "Default2 "   %>

<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html   xmlns= "http://www.w3.org/1999/xhtml "   >
<head   runat= "server ">
        <title> 无标题页 </title>


</head>
<body>
        <form   id= "form1 "   runat= "server ">
        <div>
                <asp:Label   ID= "Label1 "   runat= "server "   Width= "149px "> </asp:Label>
                <br   />
                <br   />
                <asp:Label   ID= "Label2 "   runat= "server "   Width= "149px "> </asp:Label>
                <br   />
                <br   />
                <br   />
                <asp:Label   ID= "Label3 "   runat= "server "   Width= "147px "> </asp:Label>
                <br   />
                <br   />
                <asp:Label   ID= "Label4 "   runat= "server "   Width= "147px "> </asp:Label>
                <br   />
                <br   />
                <br   />
                <asp:Label   ID= "Label5 "   runat= "server "   Width= "145px "> </asp:Label> <br   />
                <br   />
                <asp:Label   ID= "Label6 "   runat= "server "   Width= "141px "> </asp:Label> </div>
        </form>
</body>
</html>


[解决办法]
vau是类的成员属性啊, str2 = str1,str2 = "str1 xxx "; "str1 xxx "其实也是当做一个引用类型,str2的引用都变了,当然输出就和Str1不同了。 str1 和 “str1 xxx”是等价的
[解决办法]
string 是不可变的对象,你给它重新赋值的时候它已经不是原来的那个呀
要实现上面的效果要用stringbuilder 对象
[解决办法]
string经过了特殊处理,每次改变值时都会建立一个新的string对象,变量会指向这个新的对象,而原来的还是指向原来的对象,所以不会改变。这也是string效率低下的原因,如果经常改变string的值则应该使用StringBuilder而不使用string
[解决办法]
"= "号对于string已经不是普通的引用赋值的意义了

应该是重载了=号操作符,具体没研究过

一看见=号,就应该是实例化另一个string对象,里面的值是一样的,但引用地址不同了



[解决办法]
string 是特殊的引用类型...

尽管 string 是引用类型,但定义相等运算符(== 和 !=)是为了比较 string 对象(而不是引用)的值。这样做是为了使对字符串相等性的测试更为直观。

string 对象称为不可变的(只读),因为一旦创建了该对象,就不能修改该对象的值。看来似乎修改了 string 对象的方法实际上是返回一个包含修改内容的新 string 对象。如果需要修改字符串对象的实际内容,请使用 System.Text.StringBuilder 类。

热点排行