C# puzzle of the day
Lately I’ve been exploring old prototype code I wrote year ago. It always amazes me to se how my coding style changes over the years. It also intrigues me, because it’s a kind of cross between archeology and journaling. I can see what I was doing years ago. I wrote this little C++ memory simulation back in 2002, for a the memory component of a little CPU simulator:
struct memory
{
byte_t* mem;
unsigned int size;
`memory(unsigned int x = vpc::mem_size)
{
mem = new byte_t[x];
size = x;
//memset(mem, 0, size);
}
~memory()
{
if(mem != 0)
{
delete mem;
mem = 0;
size = 0;
}
}
template
template
template
I like this because it illustrates how a strongly typed language can be a hindrance. Last time I posted an entry criticising C# for lacking the power of C++, I got back some very intriguing responses that showed that with sufficient ingenuity, you can get around most hurdles in a language. So this time, I’m wondering how we could overcome this strong typing constraint. I know I’m a hypocrite for eulogising about strong typing in one post, and then criticising it in the very next. I guess I just want the power to not be bound by it, if I know what I am doing.
This must be an issue for C# communications libraries where they need to efficiently convert a byte stream into an object. Since all objects and classes are ultimately composed of primitives, I have to assume that recursive application of the BitConverter class is enough to impose shape on the data stream - but you would have to assign an object on a heap to store the data, so you can’t do what is done above.
How would you do this in C#?
Andrew Matthews
Computer Science