c# - How to clone part of a List? -
is possible clone part of list<t>
?
example:
list<string> myoriginalstring = new list<string>(); myoriginalstring.add("tyrannosaurus"); myoriginalstring.add("amargasaurus"); myoriginalstring.add("mamenchisaurus");
i want clone myoriginalstring
list index 1
index 2
.
is possible? changes in sec list<string>
should reflected in first , vice-versa.
thanks answers far. seems didn't express myself correctly.
actually don't want re-create or clone. need create new list (which part of original one); , when alter (some value) in new list, original should changed same way. (the lists should identical time, new list part of original).
hopefully clearer.
you can create listslice<t>
class represents piece of existing list. piece behave read-only list , because keeps reference original list not supposed add together or remove elements in original list. cannot enforced unless implement own list not here.
you have implement entire ilist<t>
interface including ienumerator<t>
need enumerating slice. here example:
class listslice<t> : ilist<t> { readonly ilist<t> list; readonly int32 startindex; readonly int32 length; public listslice(ilist<t> list, int32 startindex, int32 length) { if (list == null) throw new argumentnullexception("list"); if (!(0 <= startindex && startindex < list.count)) throw new argumentexception("startindex"); if (!(0 <= length && length <= list.count - startindex)) throw new argumentexception("length"); this.list = list; this.startindex = startindex; this.length = length; } public t this[int32 index] { { if (!(0 <= index && index < this.length)) throw new argumentoutofrangeexception(); homecoming this.list[this.startindex + index]; } set { if (!(0 <= index && index < this.length)) throw new argumentoutofrangeexception(); this.list[this.startindex + index] = value; } } public int32 indexof(t item) { var index = this.list.indexof(item); homecoming index == -1 || index >= this.startindex + this.length ? -1 : index - this.startindex; } public void insert(int32 index, t item) { throw new notsupportedexception(); } public void removeat(int32 index) { throw new notsupportedexception(); } public int32 count { { homecoming this.length; } } public boolean isreadonly { { homecoming true; } } public void add(t item) { throw new notsupportedexception(); } public void clear() { throw new notsupportedexception(); } public boolean contains(t item) { homecoming indexof(item) != -1; } public void copyto(t[] array, int32 arrayindex) { (var = this.startindex; < this.length; += 1) array[i + arrayindex] = this.list[i]; } public boolean remove(t item) { throw new notsupportedexception(); } public ienumerator<t> getenumerator() { homecoming new enumerator(this.list, this.startindex, this.length); } ienumerator ienumerable.getenumerator() { homecoming getenumerator(); } class enumerator : ienumerator<t> { readonly ilist<t> list; readonly int32 startindex; readonly int32 length; int32 index; t current; public enumerator(ilist<t> list, int32 startindex, int32 length) { this.list = list; this.startindex = startindex; this.length = length; } public t current { { homecoming this.current; } } object ienumerator.current { { if (this.index == 0 || this.index == this.length + 1) throw new invalidoperationexception(); homecoming current; } } public boolean movenext() { if (this.index < this.length) { this.current = this.list[this.index + this.startindex]; this.index += 1; homecoming true; } this.current = default(t); homecoming false; } public void reset() { this.index = 0; this.current = default(t); } public void dispose() { } } }
you can write extension method create easier work slices:
static class listextensions { public static listslice<t> slice<t>(this ilist<t> list, int32 startindex, int32 length) { homecoming new listslice<t>(list, startindex, length); } }
to utilize piece can write code this:
var list = new list<string> { "tyrannosaurus", "amargasaurus", "mamenchisaurus" }; var piece = list.slice(1, 2); slice[0] = "stegosaurus";
now list[1]
slice[0]
contains "stegosaurus"
.
c# .net list clone
No comments:
Post a Comment