"steve bull" <bu****@comcast.net> wrote in message
news:a7********************************@4ax.com...
unfortunately the indexer cannot have a name so the index is on the class as a whole. which pretty
much seems like a
waste of time. MS don't appear to have spent too much time thinking about this.
This is true, however, If you really want a "Readonly Array", you can roll your own....sort of.
If you don't need all of the bells an whistles of the Array class, you can make a pseudoArray quite
easily.
This probably should be generic, but you get the idea
This is a bare minimum
// Minimal version
public class ReadOnlyIntArray
{
private int[] data;
public ReadOnlyIntArray(int[] data)
{
this.data = data;
}
public int this[int idx] {get{return(data[idx] );}} // didn't even bounds check
public int Length {get{return(data.Length);}}
}
This works fine for Accessing elements via indexer, but you can't do foreach since in does not
support IEnumerable.
If you want foreach support you can do this
// IEnumerable, IEnumerator version
public class ReadOnlyIntArray : IEnumerable, IEnumerator
{
private int currentIdx = -1;
private int[] data;
public ReadOnlyIntArray(int[] data)
{
this.data = data;
}
public int this[int idx] {get{return(data[idx] );}} // didn't even bounds check
public int Length {get{return(data.Length);}}
public IEnumerator GetEnumerator()
{
Reset();
return (this);
}
public object Current {get {return data[currentIdx]; }} // needs bounds check
public bool MoveNext()
{
bool ret = true;
currentIdx++;
if (currentIdx == data.Length)
ret = false;
return (ret);
}
public void Reset()
{
currentIdx = -1;
}
}
These classes were quick and dirty and need a bit of work to be more robust and behave correctly,
but you get the idea.
Hope this helps
Bill