| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1320 人关注过本帖
标题:关于引用类型和值类型的区别 一直弄不明白 达人解释
取消只看楼主 加入收藏
达文西
Rank: 1
等 级:新手上路
威 望:1
帖 子:117
专家分:0
注 册:2007-8-4
收藏
 问题点数:0 回复次数:3 
关于引用类型和值类型的区别 一直弄不明白 达人解释

using System;
class Reftyperectangle
{
public int Width;
public int Height;
}
struct Valtyperectangle
{
public int Width;
public int Height;

}

class Refvaltest
{
public static int Main()
{
Reftyperectangle rec1 = new Reftyperectangle();
rec1.Width = 15;
rec1.Height = 10;
Reftyperectangle rec2 = rec1;
Console.WriteLine(rec2.Width+"*"+rec2.Height);
rec1.Width = 25;
rec1.Height = 20;
Console.WriteLine(rec2.Width + "*" + rec2.Height);


Valtyperectangle rec3 = new Valtyperectangle();
rec3.Width = 10;
rec3.Height = 15;
Valtyperectangle rec4 = rec3;
Console.WriteLine(rec4.Width+"*"+rec4.Height);
rec3.Width=25;
rec3.Height=30;
Console.WriteLine(rec4.Width+"*"+rec4.Height);
Console.ReadLine();
return 0;

}

}


这道题运行结果是 15*10
25*30
15*10
15*10
using System;

class stringexample
{
public static int Main()
{
string s1 = "A string";
string s2= s1;
Console.WriteLine(s1);
Console.WriteLine(s2);
s1="Another string";
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.ReadLine();

return 0;

}
}

这道题运行结果是 A string
A string
Another string
A string
我快晕了
达人快来解释阿



搜索更多相关主题的帖子: 类型 解释 
2007-08-23 14:01
达文西
Rank: 1
等 级:新手上路
威 望:1
帖 子:117
专家分:0
注 册:2007-8-4
收藏
得分:0 
回复:(达文西)关于引用类型和值类型的区别 一直弄...
对阿  
2007-08-23 14:35
达文西
Rank: 1
等 级:新手上路
威 望:1
帖 子:117
专家分:0
注 册:2007-8-4
收藏
得分:0 
以下是引用virusplayer在2007-8-23 14:44:22的发言:


引用传递的时候2个实例同时指向内存的同一个地址


值传递的时候2个实例在内存中各自有自己的地址

这个我理解了

[此贴子已经被作者于2007-8-23 17:30:46编辑过]

2007-08-23 17:27
达文西
Rank: 1
等 级:新手上路
威 望:1
帖 子:117
专家分:0
注 册:2007-8-4
收藏
得分:0 
以下是引用立志成佛在2007-8-23 14:55:00的发言:
string 这个类型比较特殊

如果你两次定义的时候,赋值都一样的话只会建立一个实例,两个都引用这个实例。
如果你定义不同的时候,则会新建立一个实例,然后两个指向不同的实例。

string是只读的,每次有变化,必须在内存中重新分配地址存放新的值。

这样解释正确吗?请问大家 说string这个类型比较特殊

2007-08-23 17:30
快速回复:关于引用类型和值类型的区别 一直弄不明白 达人解释
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.077449 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved