Sign in to follow this  
Tokage

C++ and C#

Recommended Posts

I've been writing Windows games in C++ for several years now, and I've just recently taken the time to start looking into the .NET framework. I picked up C# failry quickly (it isn't exactly hard) and I've been pondering a switch. So, my question to those of you with the experience to answer the question: What are the advantages of using the .NET framework when compared to writing native code? Is it slower in any appreciable way? If I do decide to switch to .NET, should I also switch to C#?

Share this post


Link to post
Share on other sites
Or C++/CLX (that may not be what it's called -- I can't remember).

I personally feel C# and C++ are both convenient for different things. C# is best for Windows based applications in my opinion; C++ is good when you need lots of low-level control, or just need to squeeze literally every ounce of speed possible (unlikely).

Other than that, though there are valid arguments why C# or C++ are better for any given task, neither is considerably better than the other, so I think it's a matter of opinion.

If you like C# better, use it.

If you like C++ better, use it instead.

Share this post


Link to post
Share on other sites
You're never going to get a clear answer about x vs. y language. [smile]

But why not use C++.NET?
.NET is very convenient if you can afford Visual Studio. You have access to an extensive common library of useful functions that are compatible between the .NET languages.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tokage
What are the advantages of using the .NET framework when compared to writing native code?
Pervasive memory/resource management. Cleaner and simpler access to system services (I presume you've manually added a ReBar control to a toolbar using Win32 before, so you'll really appreciate the ease of WinForms). Language agnosticism and seamless reuse of components developed in other languages (the COM-type abstraction is fully automated and hidden from you via the .NET Framework). Powerful "language features" provided by the runtime, especially reflection. Powerful ancillary services like versioning, configuration and metadata.

Quote:
Is it slower in any appreciable way?
Life is about tradeoffs. Of course it's marginally slower.

Quote:
If I do decide to switch to .NET, should I also switch to C#?
Not necessarily. C++.NET is a current option, but C++/CLI is just around the bend, and there has been some cross-pollination of ideas between the C++/CLI developers (ie Microsoft guys) and C++ Standards Committee. There is no penalty in .NET for using one language over the other, though some syntactic constructions may struggle adapting themselves to .NET.

Personally, I prefer to code without braces.

Share this post


Link to post
Share on other sites
I use both C++ and C# full time, and if I have to do anything other than crunch numbers/data I am a lot happier using C#. It is simply easier to interface with a system, get what I need, do what I want, etc. GUIs can be quickly developed, databases can be connected to and used without much trouble, and the .Net Framework itself is a vast array of reusable code.

Once you learn C# you can write Applications that are write-one run-everywhere for Windows, Linux, and Mac machines. You can write applications for Windows based organizers easily. For complete cross-platform abilities, you can create web pages using whatever .Net language it is that you know, making the learning curve much less steep. I just generally find it an enjoyable experience.

Oh yeah, and I was woken up in the early morning yesterday by a phone call for a job offer because of knowing .Net, so it is definately a good resume item too.

Share this post


Link to post
Share on other sites
I'd recommend using both languages and deciding upon a language for each project (or each subcomponent of a project) depending on which language best serves your purposes at the time.

People who restrict themselves to one language tend to become dogmatic in their belief that their chosen language is superior to all others (see the recent Lisp thread).

The more languages you are comfortable with the greater choice you have when it comes to picking a suitable language for a given task.


Specifically in answer to your question, managed .NET code is significantly slower than unmanaged (up to 10% although, of course, it depends what you're doing with it) but you get significant benefits for that performance hit. At the end of the day, it's your responsiblity to weigh up the pros and cons for a specific task.

Share this post


Link to post
Share on other sites
The *best* way to understand the differences between languages is to just *personally* experiment with both of them, learn where the advantages and disadvantages are. Basing decisions on other people's "facts" that aren't backed up by cold, hard evidence is never a good policy.

If you really want to understand the exact differences in performance of simple but common C++ and C# operations (like virtual function calls or array indexing, for instance), write the exact same thing in both languages, compile with full optimizations (if relevant), launch each program, attach a debugger and look at the machine code (you can do this for the C# app even though the .exe contains non-native instructions - the Disassembly view in Visual Studio shows you the native disassembly for your program). The important part of this examination is that you use the best optimization available. You don't want to know what the unoptimized code looks like because that's not what you're going to end up giving to the customer.

When you find the assembly instructions in memory for the given operation, only then can you *exactly* evaluate how a simple operation differs between the two languages. If they're the same, then they're the same. If the C# executable has an extra array bounds check, then that's what makes it slower. With that knowledge, you can then decide whether you want to use C++, C#, or a mix of the two.

Something that becomes common in language comparison threads is "write a benchmark". You shouldn't write a benchmark for a given operation and expect to learn WHY something is faster/slower when it's implemented a certain way in a high level language. In the end, it's not very useful -- and even possibly flawed -- information. The machine code is what's getting executed in the end, and that's absolute.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by intrest86
Once you learn C# you can write Applications that are write-one run-everywhere for Windows, Linux, and Mac machines.


Are there any examples of that? Afaik M$ just says that so that ignorant people won't think they're trying to force their OS even more :|

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by intrest86
Once you learn C# you can write Applications that are write-one run-everywhere for Windows, Linux, and Mac machines.


Are there any examples of that? Afaik M$ just says that so that ignorant people won't think they're trying to force their OS even more :|


It's not quite that simple, but it is becoming more of a reality. I use Linux to develop cross-platform .Net apps and that works fine as write once / run everywhere. I don't use Windows.Forms, though, which is what makes GUI programming so easy in .Net, and that isn't doing nearly as well on other OSes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Are there any examples of that? Afaik M$ just says that so that ignorant people won't think they're trying to force their OS even more :|


Both the Mono and DotGNU projects are fairly mature. Although DotGNU has some outstanding issues that still need to be resolved both are very usable and both environments are gaining support from other development projects (IDE's, runtime libs, ports, etc). The Mono project is the more popular of the two, has it's own C# compiler (written IN c#), an IDE (MonoDevelop), and there several compilers that target the CLI. The Mono C# compiler conforms to the ECMA-334 language specs and the runtime is compliant with the ECMA-335 specs. This means that you can Mix C#, javascript, Python, and other languages without the mess of custom bindings.

With the Mono runtime using a GNU license you can distribute a standard runtime or make a custom runtime to fit your requirements AND have your application run without your users having to download [the bloated] MS.NET.

Share this post


Link to post
Share on other sites
I don't know if it's bloat so much as it IS the kitchen sink. I've used a sizable portion of it, and would probably be ticked if any of it was removed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this