关注联楷|返回首页 | 网站案例 | 帮助中心

合作共赢、快速高效、优质的网站建设提供商

上海网站建设 

服务热线:021-61394118

上海联楷科技

如何使用引用类型来改变变量的值

作者:佚名   时间:2013-11-07   分享到:

引用类型总是从托管堆上分配,c#的new操作符返回的就是对象位于托管堆中的内存地址-这个内存地址指向对象占用的数据位,将一个引用类型的实例赋值给另一个引用类型的实例就是将这个实例的指针赋给另一个实例,,也就是说赋值后两个实例指向同一个内存块,改变一具实例的值自然就改变另外一个实例的内容.
   先定义一下类,代码如下:
class hello
{
  private string hellotxt;
  public  string hellotxt;
{
  get { return this.hellotxt; }
  get { this.hellotxt = value; }
}
pubile hello(string text)
 {
   hellotxt = text;
 }
}
然后使用这个类,代码如下:
hello hello1 = new hello("hello world!");
hello hello2 = hello1;
hello1.hellotxt = "hello";
console,writeline(hello.hellotxt);  //hello
   大家通过上面代码的演示就可以看到改变了hello1的属性之后,hello2的属性也自动变了.



如没特殊注明,文章均为上海联楷网络原创,转载请注明来自:http://www.linksj.com/help/20151126/n1658.html

上海联楷网络新闻