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

值类型,引用类型,该如何解决

2012-09-27 
值类型,引用类型C# code Listint date new Listint()date.Add(1)Listint temp date.GetRange(

值类型,引用类型

C# code
 List<int> date = new List<int>();            date.Add(1);            List<int> temp = date.GetRange(0, 1);            temp[0] = 2;


上面的代码不能改变date[0]的值。

GetRange是返回浅表副本,但是应该因为int是值类型,结果返回的就是一个副本了。
我想返回浅表副本怎么办呢

[解决办法]
http://topic.csdn.net/u/20120415/01/ee51f4eb-eaed-43f0-95f2-6dc5e7b70540.html

或者使用如下代码:
C# code
class SubList<T>{    private List<T> innerList { get; set; }    private int offset { get; set; }    public SubList(List<T> list, int startindex) { innerList = list; offset = startindex; }    public T this[int index] { get { return innerList[index + startindex]; } set { innerList[index + startindex] = value; } }}...List<int> date = new List<int>();date.Add(1);SubList<int> temp = new SubList<int>(date, 0);temp[0] = 2;
[解决办法]
直接copy 不知道行不行

热点排行