Linux Format logo

Why the heck did C# launch without generics?

Mike and I are almost two weeks into our little programming project, and things are progressing nicely. Scrolling is now implemented, which means you can set the screen to follow a given player on the X or Y axis (or both), and it'll do just that. The input back-end has also been rewritten to handle key repeats, which means you can specify that pressing a key will only execute an action once, so the player needs to release the key and repress it.

However, I'm really starting to get sick of the lack of generics in C#. Our game engine makes extensive use of ArrayLists (C++ vectors) and Hashtables (C++ maps), but the problem is that Mono only implements the C# 1.0 specification, which means that every time we retrieve a value from one of these collections we need to cast it to the correct type, like this:

Foo myfoo = (Foo)myarraylist[4];

Now that doesn't look too bad on the eye, but it starts to look seriously ugly when you're trying to call methods:

Foo myfoo = ((Bar)myarraylist[4]).GetFoo();

So now we're typecasting myarraylist[4] as Bar, then calling GetFoo() on it. Now let's try checking that for a null return value:

if (((Bar)myarraylist[4]).GetFoo() != null) { }

My eyes can handle two brackets, but three is where I draw the line. The situation is a lot worse if you're trying to use a collection inside a collection, as you need to typecast twice. That's beyond my bracket count, so when this crops up I do one typecast and assign it to a temporary variable, then do another typecast on that temporary variable. If C# let me force a reference copy this might not be so bad, but as it stands I can't quite tell whether this is wasting memory or it's doing a copy-on-write operation.

C# 2.0 adds generics support, which means that you can define an list as holding objects of type Bar, like this:

List<bar> myarraylist = new List<bar>();

The rest of the code remains the same, sans constant typecasting.

Microsoft's Visual Studio 2005 already supports generics in C#, and Mono's gmcs compiler claims to support generics, but I'm reticent to switch my code over just in case the Mono version isn't quite there yet. Mono 1.2 is due out soon, which should clear up this problem. In the meantime, if anyone has any experience of C# 2.0 on Mono 1.1.13, drop me a line!

And now for something completely different. A Japanese friend of mine recently gave me a small box of chocolate biscuits. Having scoffed the lot, I noticed this particularly fine cartoon on the inside lid:

A strange Japanese cartoon

Now, the question I have for you all is this: What The Flipping Heck Is Happening In The Cartoon? Post your answers on the blog forum, and in a week or so I'll pick the best/cleverest/funniest answer and send the winner an LXF t-shirt. Before you answer: note the numbering on the picture frames! Japanese people read their comic strips in a different order than westerners, and the cartoon is only likely to make sense (if it ever makes sense at all) when read in the original order. Note: if you actually can read Japanese, please don't take part - we're not interested in what the cartoon actually says ;)

Continuing the Japanese theme, Canon has released the EOS 30D digital camera, the replacement to my little 20D. It actually seems like a worthwhile upgrade, but sadly a new digital camera is beyond my spending power right now so I'll just have to content myself with laughing at their masterful grasp of English:

Speed and performance that improve than ever

And finally... today Team LXF has been drinking Pepsi Max Cino, the new coffee-flavoured, sugar-free Pepsi drink. Pepsi describes it at combining "the full-on taste of your favourite Pepsi Max with a smooth cappuccino flavour". Here's our take:

  • Graham: "It tastes like Panda Cola."
  • Nick: "It tasted like someone had distilled the worst elements of the special flavoured cola (lemon, vanilla, etc) and put them into the one bottle."
  • Gregory: "It's like a paint factory just exploded in my nose."
  • Rebecca: "It tastes horrible." (she has a way with words)
  • Mike: "It's like there's a party in my mouth and everyone's covered in tarmac."

Perhaps I should email Pepsi and tell them they misspelled "paint factory" as "cappucino".

Sayonara!


Your comments



Web hosting by UKFast