c# - Conditionally creating new collections with output parameters -
this program purely illustrative purposes. wanted take integer input , if greater zero, create arraylist integer in arraylist. tried numerous (incorrect) ways , settled on see below. however, don't looks of it.
using system; using system.collections.generic; using system.linq; using system.text; using system.collections; class program { static void main(string[] args) { console.write("enter in number of size of arraylist want: "); int arraysize = int.parse(console.readline()); if (arraysize > 0) { arraylist newlist = createlist(arraysize,out newlist); newlist.add(arraysize); console.writeline("the size of array {0}",newlist.count); console.readline(); } else { console.writeline("you did not create arraylist"); } console.writeline("here can't access array"); console.readline(); } public static arraylist createlist(int x,out arraylist outlist) { arraylist al = new arraylist(x); outlist= al; return outlist; } }
my thought process if user decided on action program wouldn't create arraylist save resources (silly example considering example, know) . however, still have initialize arraylist in main method, , if take out line newlist.add(arraysize)
program runs output of 0 if user enters number greater 0. there way make input greater 0 result in arraylist that's ready go element added? if commented out line newlist.add(arraysize)
program still print out size of arraylist 1 (provided number greater 0)
in example number of elements maximum of 1, , not number user might enter.
there's lot of things going wrong here.
firstly, can't add size of list. it's initialised in constructor, , grows , shrinks add , remove elements.
so line:
newlist.add(arraysize);
is misleading , not want. either want add element (in case variable name misleading) or want set array's size or capacity, in case code isn't doing want.
there difference between list's size , capacity should aware of:
- size number of elements in array.
- capacity size of list's underlying array, equal or greater list's size @ point in time.
this why call count returning 0 when you've initialised list other value. count refers number of items in list, not length of internal array. want call capacity property instead:
console.writeline("the size of array {0}", newlist.capacity);
lists (arraylist , list) use array's internally, of fixed size. capacity refers length of array. , when add new element list array full, list's add method creates new array double length of old array, copies elements larger array, appends new value @ end, , discards old array.
so in short, function:
public static arraylist createlist(int x,out arraylist outlist) { arraylist al = new arraylist(x); outlist= al; return outlist; }
is okay, though redundant in should either use out parameter or return list, not both. case, like:
public static void createlist<t>(int capacity, out list<t> list) { list = new list<t>(capacity); }
even though it's pretty pointless method stands.
Comments
Post a Comment