niedziela, 21 marca 2010

I've become a PS guy

Rcently, I've bought a Playstation 3 console.
It is my first modern game console that I've bought. I also own Nintendo WII, but it was a gift and the only real game that I have is Zelda (some also may say that WII is not a modern game console). When I was a kid, I had Pegasus - it was some kind of NES clone. I played Contra, Dig Dug and other great games. Then I bought a PC (yep, I'm a PC ;)) and felt superior to my playstation / sega saturn / amiga friends ;) At school we were talking about number of polygons and comparison of benchmarks between Playstation and Voodoo. Not to mention, I always felt that PC's are far better that any consoles.
Currently, I'm not so religious about it. Three years ago I upgraded GPU in my PC and most games still run quite smoothly. However, I was thinking about buying a game console. For me decision was quite easy. Imho, Playstation 3 has far better games compared to Xbox (the only game I could be missing is Gears of War). Before buying PS3, I waited for one of this games to be realeased: Heavy Rain, God Of War 3 and Final Fantasy XIII. I've already finished Heavy Rain and currently I'm playing GOW3. I've got only one thing to say: PC guys - buy yourself a PS3! ;)

System.String and new() constraint

Few days ago, my colleague was dealing with code that was loading a list of objects from DataReader. Basically, for every record, a new object was created like this:
T obj = new T();
//set some properties using reflection
result.Add(obj);
Of course, the method had new() constraint for type T. The method name was pretty generic, however the method couldn't handle scalar values like ints, floats, etc. (because it tried to set some properties through reflection). My colleauge changed the code and if the value was a scalar value (IsPrimitive seemed ok for us) the value from reader was casted to T and assigned to obj. My colleague mentioned that this method won't work for String because String doesn't have public parameterless constructor. To be honest, I didn't know that because I have never needed to write something like String str = new String(). I've always written String str = String.Empty. However, it seems really odd that you can't just say new String(). I asked about this issue on stackoverflow: http://stackoverflow.com/questions/2454914/system-string-why-doesnt-it-implement-parameterless-constructor.
I suggest you read this post. Discussion in answers is really interesting.

I asked this question and thought about this issue for few hours. In our particular case, the code was clearly wrong. We created another method for scalar values without new() constraint. However, I still think that lack of public parameterless constructor for System.String is a .NET flaw (yet, as I said, I have never had a need to use it ;)).