deriving from Byte[] in C#

G

Guest

Hi all,
is it possible to derive from an array type - in my case from Byte[]. The
purpose is to override the ToString() of Byte[] method in such a way that it
results in a string representing the Byte[] as a 'normal' string - i know
that only legal ASCIIs are in the array - and not resulting in
"System.Byte[]".
Thanks in advance
 
J

Jani Järvinen [MVP]

Hello!
Is it possible to derive from an array type - in my case from Byte[]. The
purpose is to override the ToString() of Byte[] method in such a way that
it
results in a string representing the Byte[] as a 'normal' string.

Unfortunately, the short answer is no. By looking at the documentation for
Array, it might appear that you could derive your own types from
System.Array, but in fact all (at least all I have used) .NET
languages/compilers refuse to allow this. For example, the C# compiler fails
with the error message is "NN cannot derive from special class
System.Array".

However, even if you could do this, deriving would be more difficult than it
sounds at first, since the compiler actually does quite a lot of so-called
"compiler magic" for you when you use a seemingly simple construct like
this:

byte[] myBytes = { 12, 83, 62 };

Since System.Array is an abstract class, the compiler must first create a
derived type behind the scenes. Also, there's code needed to initialize the
array -- you can see all this when you use ILDASM for example to view the
generated MSIL (intermediate language) code.

Finally, if you would derive your own byte array class, it would not work
the same as regular arrays. As noted by the MSDN documentation of the Array
class:

"However, only the system and compilers can derive explicitly from the Array
class. Users should employ the array constructs provided by the language."

So the bottom line is that you cannot do it. With forthcoming C# 3.0
extension methods you could make things look more convenient, but in the end
your option is to write some code to convert the byte array to a string. See
here for more information extension methods in C# 3.0:

http://msdn2.microsoft.com/en-us/library/ms364047(vs.80).aspx#cs3spec_topic3

Finally, here's a simple example of a method that converts a byte array to a
proper string:

-----------------
byte[] myBytes = { 72, 101, 108, 108, 111, 33 };
string str = System.Text.Encoding.ASCII.GetString(myBytes);
MessageBox.Show(str);
-----------------

This would show: "Hello!". If you wanted to get the real bytes instead, then
you could use something like this:

-----------------
MessageBox.Show(BytesToString(myBytes));

private string BytesToString(byte[] bytes)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("{ ");
foreach (byte b in bytes)
{
if (buffer.Length > 2) buffer.Append(", ");
buffer.Append(b);
}
buffer.Append(" }");
return buffer.ToString();
}
-----------------

This would show: "{ 72, 101, 108, 108, 111, 33 }".

Hope this helps!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top