Chinaunix首页 | 论坛 | 博客
  • 博客访问: 501725
  • 博文数量: 704
  • 博客积分: 39800
  • 博客等级: 大将
  • 技术积分: 4950
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 13:32
文章分类

全部博文(704)

文章存档

2011年(1)

2008年(703)

我的朋友

分类:

2008-10-15 13:43:27

在msdn上

值类型主要由两类组成:

结构

枚举

结构分为以下几类:

Numeric(数值)类型

整型

浮点型

decimal

bool

用户定义的结构。

也就是string属于引用类型,但是实际使用中

class Program
{
static void StringCon(string str)
{
str="New String";
}

public static void Main(string[] args)
{
Console.WriteLine("Hello World!");

// TODO: Implement Functionality Here
string oldstr ="str";
Console.WriteLine("before:{0}",oldstr );
StringCon(oldstr );
Console.WriteLine("after:{0}",oldstr );
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
输出:before:str
after:str

也就是没有引用类型的应该有的表现。这是为什么了?

实际上。net设置string,有个事实就是string一旦建立,他的值是不可改变的,而当将一个已经存在的字符串赋予新值的时候,实际上在次过程中分配了一个新字符串(原有的字符串将被GC回收)

static void StringCon(string str)// 相当于string str =oldstr
// str是oldstr的一个副本,他们只是指向相同的地址
{
str="New String";// 相当于string str =new string("New String";)
// str现在的地址改变了,而oldstr还是原先的地址
}
所有string在当作参数传递是有值类型的表现,但是实际还是引用类型

【责编:Luzi】

--------------------next---------------------

阅读(328) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~