C# arrays
-
Ok, something worries me about array types in C#. Two things actually, the first being the concrete type of an array of C# objects. The following:
SomeClass[] myArr = new SomeClass[10];
creates an object
myArr
that is of a type derived fromSystem.Array
, and specialised for objects of typeSomeClass
. My first question regards the[]
operator. Where does it suddenly spring into the picture? It doesn't exist on objects of typeSystem.Array
. The type of the indexer formyArr
above isSomeClass
it seems, as the compiler won't let me make a statement like:myArr[0] = new string;
and quite rightly so, complaining that
string
cannot be converted intoSomeClass
. Thus the indexer must be part of the interface of the automatically generated derived array type. The wierdness begins when I do this:object[] mySameArr = myArr; Console.Write (mySameArr[0]);
Now, this compiles and runs without a problem. How? The interfaces of the derived
object[]
andSomeClass[]
types must be different, because each declares an indexer with a different signature. This probably shouldn't compile, or at least, shouldn't run, because despiteSomeClass
being derived fromobject
,SomeClass[]
is NOT derived fromobject[]
! What's going on? Does the C# compiler fudge this some way, or does the .NET CTS have different relationships between array types that doesn't fit my (somewhat C++ skewed) view? Is this some kind of mock-up of generics that seems like it will be broken once real generics are implemented? My second, and maybe not so confusing, question regards pointers and arrays. This appears in MSDN:public unsafe int Read(byte[] buffer, int index, int count) { int n = 0; fixed (byte* p = buffer) { ReadFile(handle, p + index, count, &n, 0); } return n; }
Can anyone tell me where the conversion is made between the
byte[]
specialisation ofSystem.Array
andbyte*
? Is there a conversion operator defined in that subclass, or is there some other compiler funk going on? Should I be poring through the C# standard? Thanks for any help, and please, if this has come up before (as I'm sure it would have) point me towards the old posting. -
Ok, something worries me about array types in C#. Two things actually, the first being the concrete type of an array of C# objects. The following:
SomeClass[] myArr = new SomeClass[10];
creates an object
myArr
that is of a type derived fromSystem.Array
, and specialised for objects of typeSomeClass
. My first question regards the[]
operator. Where does it suddenly spring into the picture? It doesn't exist on objects of typeSystem.Array
. The type of the indexer formyArr
above isSomeClass
it seems, as the compiler won't let me make a statement like:myArr[0] = new string;
and quite rightly so, complaining that
string
cannot be converted intoSomeClass
. Thus the indexer must be part of the interface of the automatically generated derived array type. The wierdness begins when I do this:object[] mySameArr = myArr; Console.Write (mySameArr[0]);
Now, this compiles and runs without a problem. How? The interfaces of the derived
object[]
andSomeClass[]
types must be different, because each declares an indexer with a different signature. This probably shouldn't compile, or at least, shouldn't run, because despiteSomeClass
being derived fromobject
,SomeClass[]
is NOT derived fromobject[]
! What's going on? Does the C# compiler fudge this some way, or does the .NET CTS have different relationships between array types that doesn't fit my (somewhat C++ skewed) view? Is this some kind of mock-up of generics that seems like it will be broken once real generics are implemented? My second, and maybe not so confusing, question regards pointers and arrays. This appears in MSDN:public unsafe int Read(byte[] buffer, int index, int count) { int n = 0; fixed (byte* p = buffer) { ReadFile(handle, p + index, count, &n, 0); } return n; }
Can anyone tell me where the conversion is made between the
byte[]
specialisation ofSystem.Array
andbyte*
? Is there a conversion operator defined in that subclass, or is there some other compiler funk going on? Should I be poring through the C# standard? Thanks for any help, and please, if this has come up before (as I'm sure it would have) point me towards the old posting.For anyone interested, http://www.jaggersoft.com/csharp_standard/19.5.htm[^]