日期:2014-05-17  浏览次数:20879 次

值类型,引用类型
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 不知道行不行