Of caurse, what means small? But image following code:
class MyClass{
string[] lines;
public MyClass(Stream content){
StreamReader sr = new StreamReader(content);
for(string line=sr.ReadLine(); line != null; line=sr.ReadLine())
AddLine(line);
sr.Close();
}
public void AddLine(string line){
stirng[] copy = new string[lines.lenght];
lines.CopyTo(copy,0);
copy[lines.lenght] = line;
lines = copy;
}
}
If you would read a file line-by-line into this class, it would not perform
good. In this case it would be better to use an ArrayList, but here it would
be ok:
class MyClass2{
string[] lines;
public MyClass2(Stream content){
ArrayList al = new ArrayList();
StreamReader sr = new StreamReader(content);
for(string line=sr.ReadLine(); line != null; line=sr.ReadLine())
al.Add(line);
sr.Close();
lines = (string[])(al.ToArray(typeof(string)));
}
}
It will be executed only once.
GP
"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
I guess the trouble is though what is the definition of small? I would err
on the side of type safety and only if a performance was shown to be a
problem would I consider an ArrayList being exposed. I know what you mean
though :-)
Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:uh****************@TK2MSFTNGP10.phx.gbl... If you do this with small arrays, it's ok (I also do this in that case),
but if the Array rows image that the elements must be copied on each call.
In this case it would be better to use an ArrayList (or an own, strong
typed, collectionclass)
GP
"Lee Alexander" <lee@No_Spam_Please_Digita.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl... Just to add the other way to do it is to reallocate the array to the
new size and copy the old arrays contents into the new one. Saying that I
would use the ArrayList approach anyway in a situation where I don't know
the number of elements in advance. If you have then finished the operation of adding items to it then you can *convert* the ArrayList into a
strongly type array like so:
AType items[] =(AType[]) myArrayList.ToArray( typeof( AType ) );
Regards
Lee
"Günter Prossliner" <g.**********@gmx.at> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
> Use an ArrayList. Arrays always are fixed - size in C#
>
> GP
>
> "reb" <gb***@yahoo.com> wrote in message
> news:0b****************************@phx.gbl...
> > Hi,
> >
> > How do i increase the size of the array without losing the
> > data of that array in c#?
> >
> > thanks
>
>