XNA and C#: impressions after one and a half weeks

Everything todo with programming goes HERE.
Post Reply
User avatar
Z-Man
God & Project Admin
Posts: 11624
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

XNA and C#: impressions after one and a half weeks

Post by Z-Man »

Bottom line first: both are absolutely awesome. There's ton of reasons to bash Microsoft for, but .NET in general and C# in particular are incredibly well designed.

There's not much more to say about XNA: stuff just works. It's the Python philosophy of batteries included: you have content processing, model rendering, sound and music, basic collision classes, 2d sprite rendering including fonts all built in already and no matter what kind of game you're making, you can have a prototype up in a day or two.

So, C#. The short version is that it's C++ with most of the stuff you can shoot your leg off removed and a ton of convenience candy added. Most useful stuff added:
- Delegates: function pointers done right
- Events: well, sort of glorified lists of delegates, but convenient anyway
- Properties: make your stuff look exactly like public member variables, but use set/get functions under the hood
- Interfaces: kind of what pure virtual classes are in C++, only acknowledged as such
- Standard interfaces for a lot of things, like serialization, iteration and containers
- Reflection.
- Well defined initialization order. Static class members (and static constructors) are initialized before the first object of a class is created and before the first static method is called.

Yeah, you get most of that stuff in C++, too, but you have to use nonstandard libraries and usually deal with quirky template syntax.

Stuff removed:
- HEADERS. I can't say enough how much time that saves.
- Along with that, long compile times.
- Implicit overloads of virtual functions. You have to specify exactly which class defines the function and which implements it.
- Implicitly passing arguments by reference. In C#, the caller has to express consent that the integer passed to the function may be modified.
- friend. Initially, I missed that one greatly, but after a bit, it became clear it's a good thing. It's too tempting to declare classes friends just to give one exclusive access to one or two members of the other. There's almost always a better way.
- const. I'm a bit on the fence about that. It adds complexity, but also value. You can emulate const with interfaces (and that's what the container interfaces do: there's IEnumerable for read access to containers and ICollection for write access), but still.
- Explicit memory management
- Multiple inheritance outside of interfaces
- Macros
- Full featured templates

You still get Generics to replace templates. They're not as powerful as C++ templates in what they can do (you can only call member functions of objects of template argument type if you specified the type needs to have a certain interface), but their usage is so much more powerful that it makes up for it. First, there cannot ever be any strange generic instantiation errors. If your generic parameter meets the declared restrictions, you can instantiate the generic. Second, and the mind boggles massively here, YOU CAN INSTANTIATE THEM AT RUNTIME. Yes. You can have a function that takes an arbitrary object as a parameter, looks up the object's type, instantiates a suitable generic for it, then lets that generic do the work.

You can just be immensely productive in C#. I wanted to have protobuf in the little XNA game I'm writing to get to know it, but couldn't get the semi-official protobuf-net to work on the XBox. I realized that Monday evening. So I decided to write my own crude version, thinking maybe I'd get it to handle ints and floats properly and be done then. Now it's two days later (plus sleep) and my little thing supports all primitive types and many of the XNA classes, ALL containers (Well, I still have to figure out how to handle containers of containers properly), structs and classes, it doesn't care whether elements are implemented as fields or properties, it handles inheritance, it can be easily extended, it can automatically only store those elements that haven't changed relative to a reference, handles NULL references and empty containers, it's extremely flexible (all integer types are alike, all containers are alike and can be swapped freely) and uses less bytes than protobuf. And it can store object spaghetti just fine, so it can be used as an easy savegame system.

And then there's Visual Studio. While I admit I'm not a fan of IDEs and two EMACS windows side by side suit me fine, thank you very much, I have to say that the automatic completion and checking stuff as you type it (which would never be possible in this quality with C++) and the refactoring functions, while a tad limited, are still quite useful. Remember that time when you had an oddly named function, but couldn't rename it because it would lead to changes all over the place and global search/replace wouldn't work because of that other function with the same name (where it truly fits)? Well, here you can tell the IDE to rename the function for you and do all the required changes. (Yeah, that's also possible in Eclipse. Provided you write Java.)

Comparisons with other languages you may like:
- Java. Haven't looked at that in recent revisions and it has improved a lot (also has Generics), but my uninformed impression is that C# does whatever Java does, only more, and better. (Yeah, yeah, cross platform support. /me points at Mono. Even available on the Wii.)
- Python. While it's initially way easier to write code in Python and it's a great language for what it aims at, it's simply easier to do reasonably complex things in a more restrictive environment. Have you ever debugged Python? I rest my case.

What I don't like so much is rather ignorable:
- There's the standard Windows development problem that there's no canonical place for development libraries. It's less bad than in C/C++ where your project also has to find the headers, though.
- The garbage collector is said to kick in every second or so and taking 20+ms to complete, blocking your app, if you're not careful. Haven't experienced that myself so far, really.
- Yeah, the speed loss. Especially on the XBox, where it's about a factor of 10 slower than it should be.

And before the obvious question comes up: No, no straight Armagetron port for the 360 is in the works. It wouldn't be possible. XBox apps are only allowed to communicate over the LIVE network, general sockets are not available. And without the ability to jump on all those awesome servers, what would Armagetron be? BUT: Of course, my test project is a Tron Lightcycle clone. It's my default test project :)
User avatar
Lucifer
Project Developer
Posts: 8683
Joined: Sun Aug 15, 2004 3:32 pm
Location: Republic of Texas
Contact:

Re: XNA and C#: impressions after one and a half weeks

Post by Lucifer »

My impression of C# was that it was basically Microsoft Java. I.e. do everything as much like Java as possible without getting sued by Sun.

The questions I have about C# revolve more around how mature mono is than anything else. I've been paranoid for awhile that if I got into it, I'd be dependent on Microsoft in ways that I'm not willing to accept.
Image

Be the devil's own, Lucifer's my name.
- Iron Maiden
User avatar
Z-Man
God & Project Admin
Posts: 11624
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Re: XNA and C#: impressions after one and a half weeks

Post by Z-Man »

As far as the core language is concerned, Mono implements everything. The basic system libraries are also complete. And since they're standardised now, there's not really any way Microsoft can sue them away successfully. Where you can run into problems is the user interface code. Winforms is not complete yet. Of course, it should be compatible the other way round, if you mainly develop with Mono and use Winforms, it's likely the result will run on Windows, too, and there's bindings to GTK+ you can use instead. Game relevant libraries are awkward, though. There's the Tao framework, basically wrapping SDL and OpenGL, but it doesn't hide the gritty truth that you're operating on pointers to C things very well. Still, Eufloria, a recent indie title, uses that, at least I spotted the relevant files in their installation. I can't say whether the extra work you have to do to make things work cross platform offsets the ease of development.

For the 'it's like java' thing, it sure is. But it does away with some of Java's flaws, like forced exception specifications. There's more I have forgotten, I haven't worked a lot with Java. And keep in mind that at the time C#/.Net were created, Java was solely controlled by Sun, which isn't that much better than something solely controlled by Microsoft.
User avatar
Lucifer
Project Developer
Posts: 8683
Joined: Sun Aug 15, 2004 3:32 pm
Location: Republic of Texas
Contact:

Re: XNA and C#: impressions after one and a half weeks

Post by Lucifer »

Well, I stayed off the java bandwagon for the same reason. :)

There's a lot of call for C# programmers in Austin, so I'm really curious if I should spend the time to learn it, and if it's enough like Java, I might be able to pick both up.

On the other hand, I'm not interested in programming professionally unless it's in the aerospace industry, and without particular college degrees, I can't do that (and it's one of the few areas where it actually matters ;) ). And as far as I know, the aerospace industry largely hasn't embraced java or c# or whatever latest buzzword.

I have noticed from Python that it can actually be very difficult to hide the pointers for bindings to other stuff. That's where one of the performance hits comes into play, the binding library has to work extra hard to hide underlying stuff and then make an interface that works the same as the underlying stuff. I think I've seen benchmarks with python that shows it. If you stick with the core language features (which don't get you very far), it's actually pretty fast as an interpreted language, but once you start using bindings, it slows down quite a bit.

Anyways, I'm interested in what else you turn up with your experiment. I'm surprised I missed this thread originally, I probably just saw the subject and didn't see the author. ;)
Image

Be the devil's own, Lucifer's my name.
- Iron Maiden
User avatar
Z-Man
God & Project Admin
Posts: 11624
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Re: XNA and C#: impressions after one and a half weeks

Post by Z-Man »

Lucifer wrote:the aerospace industry largely hasn't embraced java or c# or whatever latest buzzword.
There's probably a very good reason for it: in both those environments, you get unpredictable large hickups whenever the GC kicks in. That makes it impossible to guarantee response times, which I imagine is quite a no-go in that industry.
(void*)0
On Lightcycle Grid
Posts: 11
Joined: Thu Feb 11, 2010 7:28 pm

Re: XNA and C#: impressions after one and a half weeks

Post by (void*)0 »

one thing that has to been mentioned here about .NET is it's fantastic so-called "interop" APIs which means, it's abilities to link into native libraries so easily, you just have to declare or "dllimport" the dll functions and you are set up to use them. The last time I looked into this at Java, one had to write his own native wrappers for native libraries which then could be declared in Java classes - something like this, I never did that. I don't know if it's different today.

So one can easily setup a gui window in no time with .NET and put the time consuming routines into a native library written in c or c++.

And don't forget about VB.NET, it's a great language and my preferred one in the .NET environment :D
orm
On Lightcycle Grid
Posts: 13
Joined: Wed Mar 17, 2010 5:54 pm

Re: XNA and C#: impressions after one and a half weeks

Post by orm »

Z-Man wrote:There's ton of reasons to bash Microsoft for, but .NET in general and C# in particular are incredibly well designed.
I stopped there.

Noooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.

C# is nothing more that a language forced on us by Microsoft that it is touting as a "new standard".

Java is an industry standard because IT IS CROSS PLATFORM, and OPEN SOURCE!

Many standards are CROSS PLATFORM and OPEN SOURCE.

C# is PROPRIETARY and CLOSED SOURCE!

As such, it has absolutely no redeeming qualities, no matter what features it has.
Concord
Reverse Outside Corner Grinder
Posts: 1661
Joined: Sun Oct 21, 2007 5:24 pm

Re: XNA and C#: impressions after one and a half weeks

Post by Concord »

orm wrote:
Z-Man wrote:There's ton of reasons to bash Microsoft for, but .NET in general and C# in particular are incredibly well designed.
I stopped there.

Noooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.

C# is nothing more that a language forced on us by Microsoft that it is touting as a "new standard".

Java is an industry standard because IT IS CROSS PLATFORM, and OPEN SOURCE!

Many standards are CROSS PLATFORM and OPEN SOURCE.

C# is PROPRIETARY and CLOSED SOURCE!

As such, it has absolutely no redeeming qualities, no matter what features it has.
QED
User avatar
Z-Man
God & Project Admin
Posts: 11624
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Re: XNA and C#: impressions after one and a half weeks

Post by Z-Man »

orm wrote:Java is an industry standard because IT IS CROSS PLATFORM, and OPEN SOURCE!
It didn't start out like that. Sun only slowly gave up total control over it. The only difference is that Java started out truly cross-platform, and that was only because Sun only controls a marginal platform.
orm wrote:C# is PROPRIETARY and CLOSED SOURCE!
It's standardized in a way that can't be pulled back, and Mono is an open source and cross platform implementation. I don't want to go into the fiddly details of how THIS IS NOT PROPERLY OPEN!!!!!111!!!!, because I'm very well aware that it isn't, but it's good enough for me.
orm
On Lightcycle Grid
Posts: 13
Joined: Wed Mar 17, 2010 5:54 pm

Re: XNA and C#: impressions after one and a half weeks

Post by orm »

Sorry, but the bigger problem I have with C# is the whole mentality that accompanies it and what is lost when that is all that is ever taught. When you teach C# exclusively in university (as mine tried to do before I basically told them I'm using C++), you stop students from learning a key skill; they never learn memory management, and they never learn cross platform development. I talk with my classmates (who have almost no interest in C++) about stuff that could benefit them, and they don't care "Because C# does it for me." While the garbage collector may be nice, when you rely on it, you start to get lazy. When you get lazy, you get poor design, bugs, and overall performance degrades. In short, you get all the stuff we bash Microsoft over.

As for the mentality, just go into the Gamedev IRC channel and say you use C++ then just wait for them to mock you. It drives me nuts. Sure you get stuff done, but when you get right down to it, a C# game will most likely never run as fast as an unmanaged game, no matter what unmanaged language you use.

Also, Mono will most likely be killed off or boned over by Microsoft eventually anyway, and we all know it. Mono has a long... long way to go.
User avatar
Z-Man
God & Project Admin
Posts: 11624
Joined: Sun Jan 23, 2005 6:01 pm
Location: Cologne
Contact:

Re: XNA and C#: impressions after one and a half weeks

Post by Z-Man »

Ah yes. That, of course, is a more substantial complaint, but it also applies to Java. What those managed languages, when used for teaching, take away is the low level understanding of what a *pointer* is, and how things are arranged in memory. And, as has been said by someone smarter and more experienced than me, probably Joel Spolsky, some people just can't understand pointers, and those people should not be programmers. Indirection is such a fundamental concept in CS that if you don't breathe it, you should get the hell out. Therefore, I think teaching true CS should start with vanilla C to get a feeling for the machine. If you can't wrap your head around pointers, you simply fail with C. Possibly even before should come easy pseudocode mock-assembler to get even more feeling for the machine, of course.

What you say about performance also applies to the difference between C++ and Assembler. Assembler will always be more efficient. Should we use it instead, then? And it's not even entirely correct, object management heavy code can be faster in managed languages with JIT compilation and efficient GCs. What little speed advantage remains is more than compensated by the relative ease of multithreading Java and C# allow.
Also, Mono will most likely be killed off or boned over by Microsoft eventually anyway, and we all know it. Mono has a long... long way to go.
They can't, at least not the parts implementing the published standard. The winforms implementation, yes, but don't use it then.
User avatar
sinewav
Graphic Artist
Posts: 6424
Joined: Wed Jan 23, 2008 3:37 am
Contact:

Re: XNA and C#: impressions after one and a half weeks

Post by sinewav »

Thanks for this little back and forth between you two. I'm starting school soon and I'll be taking a few classes on programing. After reading this, I'm inclined to ask different questions from my instructors.
orm
On Lightcycle Grid
Posts: 13
Joined: Wed Mar 17, 2010 5:54 pm

Re: XNA and C#: impressions after one and a half weeks

Post by orm »

I will admit, I had trouble with pointers at the beginning. Eventually though, things made perfect sense and I can't see programming any other way. I like challenges, and managing pointers has to be one of the most interesting challenges of all. They can be made immensely simpler though with a shared_ptr implementation such as the one provided with the boost library. I make extensive use of those in my sprite engine, because off the guarantee that things will be deallocated when the last pointer falls out of scope. Set it and forget it till you need it... and pray to whatever god you pray to that your management system proves itself.
Post Reply