Overriding ToString() on an array?

  • Thread starter Thread starter Richard Dixson
  • Start date Start date
R

Richard Dixson

In .NET with C# code is it possible to override an arrays ToString method
with my own implementation, without having to create a new array class that
derrives from array?

For example, say I have a basic array variable with elements like this:
green
blue
red
yellow

And I want to make it so that if you call ToString() on the array it will
return a string like this "green, blue, red, yellow". Is that possible
without making my own subclass? If so how would I go about this? A tiny
piece of code demonstrating how to do this if it is possible would be great.
Thanks.

Richard
 
Richard Dixson said:
In .NET with C# code is it possible to override an arrays ToString method
with my own implementation, without having to create a new array class
that
derrives from array?

No. It is also not possible to do it even by deriving from array because you
*can't* derive from System.Array. Its a special class only compilers and the
runtime are allowed to derive from(there are others, Delegate for example).
Trying to will result in:
test.cs(3,14): error CS0644: 'Test' cannot derive from special class
'System.Array'

Unfortunatly, I think you will have to survive on helper methods, in this
case.
 
Back
Top