[Mono-list] Array Class
Nick Drochak
ndrochak@gol.com
Mon, 11 Mar 2002 17:08:26 +0900
This is a multi-part message in MIME format.
------=_NextPart_000_002D_01C1C91F.5BC0DDA0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
| Try compiling the following code, and you;ll know what I mean :)
|
| Array a =
| Array.CreateInstance(typeof(Object),5,6,7);
|
| a.Add(new Object());
| ((IList)a).Add(new Object());
|
| a.Add will throw a compile error.
|
| The documentation declares the methods I had listed as "Explicit Interface
| Implementations"
| http://msdn.microsoft.com/library/default.asp?url=/library/en-us/c
| pref/html/
| frlrfsystemarraymemberstopic.asp
|
Ajay,
Wonderful review! Thanks again.
I've updated the patch with your comments.
Nick D.
------=_NextPart_000_002D_01C1C91F.5BC0DDA0
Content-Type: application/octet-stream;
name="array.diff"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="array.diff"
Index: Array.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/class/corlib/System/Array.cs,v=0A=
retrieving revision 1.26=0A=
diff -u -r1.26 Array.cs=0A=
--- Array.cs 2002/03/08 15:40:29 1.26=0A=
+++ Array.cs 2002/03/11 04:12:00=0A=
@@ -13,8 +13,7 @@=0A=
namespace System=0A=
{=0A=
=0A=
- [MonoTODO("This should implement IList and IEnumerable too")]=0A=
- public abstract class Array : ICloneable, ICollection=0A=
+ public abstract class Array : ICloneable, ICollection, IList, =
IEnumerable=0A=
{=0A=
// Constructor =0A=
protected Array ()=0A=
@@ -45,6 +44,66 @@=0A=
}=0A=
}=0A=
=0A=
+ // IList interface=0A=
+ public object this [int index] {=0A=
+ get {=0A=
+ return GetValueImpl (index);=0A=
+ } =0A=
+ set {=0A=
+ SetValueImpl (value, index);=0A=
+ }=0A=
+ }=0A=
+=0A=
+ int IList.Add (object value) {=0A=
+ throw new NotSupportedException ();=0A=
+ }=0A=
+=0A=
+ void IList.Clear () {=0A=
+ Array.Clear (this, this.GetLowerBound(0), this.Length);=0A=
+ }=0A=
+=0A=
+ bool IList.Contains (object value) {=0A=
+ int length =3D this.Length;=0A=
+ for (int i =3D 0; i < length; i++) {=0A=
+ if (value.Equals (this.GetValueImpl (i)))=0A=
+ return true;=0A=
+ }=0A=
+ return false;=0A=
+ }=0A=
+=0A=
+ int IList.IndexOf (object value) {=0A=
+ if (this.Rank > 1)=0A=
+ throw new RankException ();=0A=
+=0A=
+ int length =3D this.Length;=0A=
+ for (int i =3D 0; i < length; i++) {=0A=
+ if (value.Equals (this.GetValueImpl (i)))=0A=
+ // array index may not be zero-based.=0A=
+ // use lower bound=0A=
+ return i + this.GetLowerBound (0);=0A=
+ }=0A=
+=0A=
+ int retVal;=0A=
+ unchecked {=0A=
+ // lower bound may be MinValue=0A=
+ retVal =3D this.GetLowerBound (0) - 1;=0A=
+ }=0A=
+=0A=
+ return retVal;=0A=
+ }=0A=
+=0A=
+ void IList.Insert (int index, object value) {=0A=
+ throw new NotSupportedException ();=0A=
+ }=0A=
+=0A=
+ void IList.Remove (object value) {=0A=
+ throw new NotSupportedException ();=0A=
+ }=0A=
+=0A=
+ void IList.RemoveAt (int index) {=0A=
+ throw new NotSupportedException ();=0A=
+ }=0A=
+=0A=
// InternalCall Methods=0A=
=0A=
[MethodImplAttribute(MethodImplOptions.InternalCall)]=0A=
@@ -111,11 +170,9 @@=0A=
}=0A=
}=0A=
=0A=
- [MonoTODO]=0A=
public virtual IEnumerator GetEnumerator ()=0A=
{=0A=
- // FIXME=0A=
- return null;=0A=
+ return new SimpleEnumerator(this);=0A=
}=0A=
=0A=
public int GetUpperBound (int dimension)=0A=
@@ -655,6 +712,51 @@=0A=
throw new ArgumentOutOfRangeException ();=0A=
=0A=
Copy (this, this.GetLowerBound(0), array, index, this.GetLength (0));=0A=
+ }=0A=
+=0A=
+ internal class SimpleEnumerator : IEnumerator {=0A=
+ Array enumeratee;=0A=
+ int currentpos;=0A=
+ int length;=0A=
+=0A=
+ public SimpleEnumerator (Array arrayToEnumerate) {=0A=
+ this.enumeratee =3D arrayToEnumerate;=0A=
+ this.currentpos =3D -1;=0A=
+ this.length =3D arrayToEnumerate.Length;=0A=
+ }=0A=
+=0A=
+ public object Current {=0A=
+ get {=0A=
+ // Exception messages based on MS implementation=0A=
+ if (currentpos < 0 ) {=0A=
+ throw new InvalidOperationException=0A=
+ ("Enumeration has not started");=0A=
+ }=0A=
+ if (currentpos >=3D length) {=0A=
+ throw new InvalidOperationException=0A=
+ ("Enumeration has already ended");=0A=
+ }=0A=
+ // Current should not increase the position. So no ++ over here.=0A=
+ return enumeratee.GetValueImpl(currentpos);=0A=
+ }=0A=
+ }=0A=
+=0A=
+ public bool MoveNext() {=0A=
+ //The docs say Current should throw an exception if last=0A=
+ //call to MoveNext returned false. This means currentpos=0A=
+ //should be set to length when returning false.=0A=
+ if (currentpos < length) {=0A=
+ currentpos++;=0A=
+ }=0A=
+ if(currentpos < length)=0A=
+ return true;=0A=
+ else=0A=
+ return false;=0A=
+ }=0A=
+=0A=
+ public void Reset() {=0A=
+ currentpos=3D -1;=0A=
+ }=0A=
}=0A=
}=0A=
}=0A=
------=_NextPart_000_002D_01C1C91F.5BC0DDA0--