I posted my code (a working program - not a snippet) to show that the OP's
problem could be solved using traditional index arrays.
That is the point.
I still don't see why that point would raise the quote: "Huh? I thought
this was the C# forum..."
What was the point of that particular paragraph, exactly? How is the
code you posted relevant to it?
"Since the Array class is the parent class of all arrays defined in C#, an
array created using C/C++ syntax has access to the methods defined in the
Array class." -- C# Programmers Reference Chapter 6
And indeed that's entirely correct. It says that System.Array is the
*parent* of int[], not the same type. You can apply indexing directly
to an int[], but not to a System.Array.
OK, so we agree that an int[] is derived from System.Array. But you say that
you cannot apply indexing to a System.Array.
Not directly, no - unless you cast to IList first. That's because it
implements IList with explicit interface implementation.
This is new to me. This is where I would like you focus your explanation.
Please explain what you mean so I can understand the difference. I don't
understand why this is, and I think a lot of people trying to move to C#
would like some detail about how this works.
Perhaps if you'd spent time looking at the code I posted instead of
saying "I thought this was the C# forum", suggesting that either I
didn't know that int[] derived from System.Array or that in some other
way my post was off-topic, you might have understood already. However,
here we go:
System.Array implements the IList.Item indexer explicitly - that's why
in MSDN it comes up as Array.System.Collections.IList.Item. (I'm not
sure why that includes the System.Collections bit but, say, Add just
comes up as Array.IList.Add.)
That means you can't the indexer on an expression of type System.Array
- you have to cast it to IList first. Note that it's the type of the
*expression* which is important here, not the type of the actual value
of the reference.
So:
Array foo = new int[5];
object x = foo[2]; // Error
object y = ((IList)foo)[2]; // Fine
Now, looking at the IL code generated for that, it really *is* calling
get_Item.
When the compiler sees an expression of type int[] (or similar) -
something it *knows* is an array - it uses different IL: ldelem and the
like.
The compiler only knows to use special indexing for what *it* thinks of
as arrays - namely int[], object[] etc. It doesn't know anything about
System.Array for indexing.
I am allowed my opinion. I think it is homework.
You're certainly allowed your opinion - but I still don't see any
*reason* to think it's a homework assignment. Lots of people want to
compare the contents of arrays, and many of them aren't doing homework.
The original array was the one given in my code (as x and y):
A = {1,2,3,4,5,6};
B = {1,2,3,4,5,6};
That's an int array!
It might be. It might not be. It might be an array of objects:
object[] A = {1, 2, 3, 4, 5, 6};
- or of bytes, shorts, longs etc.
Again, my opinion is colored by the fact that I believe it is a simple
homework assignment and the simplest case (Occam's Razor: "one should not
increase, beyond what is necessary, the number of entities required to
explain anything") would suggest that the numbers 1-6 are whole integers.
It strikes me that making the assumption that it's homework goes
against Occam's Razor to start with...
The numbers 1-6 are certainly whole integers. Whether the array that
contains them is an int[] or not is a different matter, however. In
particular, there is no guarantee that {1, 2, 3, 4, 5, 6} is an int
array. It depends on the context.
Using a method which takes a parameter of type System.Array would work
with either case - your method only works if it's declared as int[],
which we don't know.
You presented a very general discussion of stepping through an array. I even
went back and saved a copy, in case I have two complicated object arrays I
need to compare.
I still think the OP really meant an array of ints. ;-)
Maybe he did - but the answer I presented would also work as soon as he
wanted to do something slightly different. The answer you presented
would have required cutting and pasting, and then changing the type of
the array.
There is one advantage to your solution though - it doesn't require
unboxing.