C# vs. C++
Filed under Computers, Programming, Software
I am reading this document about the C# language.
Here are a list of features in C# that improve on how C++ works…
C++ | C# |
Reference to a class in another file of a project requires an include file | No include file needed |
Reference to a class in an external DLL requires an include file and the DLL’s .LIB file passed to the linker | No .LIB file needed. Add a reference to the DLL in the build(example: csc /r:external.dll helloworld.cs) |
Exported symbols must use declspec or .DEF file | No declspec or .DEF file. Public symbols are exported, private/protected are not. ”internal” symbols are only available within module. |
Referencing a class/structure before it is declared requires forward declaration | Forward declaration not needed. Declaration order is insignificant. |
Switching to Unicode/16-bit text requires:
| Unicode is the native string format |
NULL is a concept, not part of the language. It is typically defined as 0 and thus is the equivalent of 0. | null is a keyword. It is not equivalent to 0. |
Basic data types (int, float, char, etc.) cannot accept a “null” value. Only pointers can have a “null” value. | All C# types can accept null as a value
|
No support for “foreach” | foreach keyword for iterating over a collection |
Support for try and catch. No support for "finally” | try-catch-finally support. finally is always called, regardless of exception or not |
No notification of overflow operations. For example: unsigned x = UINT_MAX; x++; // x == 0 now, no exception thrown | Using checked/unchecked you can get overflow or exception thrown behavior |
No native support for critical sections | lock keyword for critical sections |
For switch statements, break is optional. | break is required (to prevent bugs from accidentally forgetting to add break) |
No built-in documentation | XML documentation via “///”- prefixed comment. Used by IntelliSense in Visual Studio |
Cannot split the definition of a class across multiple files | Partial classes allow breaking up a source file (useful when multiple people are working on a class or when part of a class is auto-generated) |
No support for mixing versions of libraries | Can mix library versions without breaking existing code. |
Developer must manage deletion of memory and potential issues from reading/writing to invalid memory | Garbage collection:
|
Types do not share a common root | All types derived from base “object” |
Comments (8)
You lost me at Hello World!
Posted by Bubba | May 13, 2009 6:53 AM
Posted on May 13, 2009 06:53
I know you posted this to bait me, but I just can't help myself.
Posted by nick | May 13, 2009 6:27 PM
Posted on May 13, 2009 18:27
Assembly is faster than C++. C++ is good enough most of the time. When it is not, you can always write it in assembly. You can do the same with C#.
The runtime slowdown disadvantage from using C# may be acceptable when you consider all the time you save devlopers building and
debugging difficult issues compared to C++:
- No memory leaks
- No memory corruption
- No unsafe code (no buffer overflow attacks)
- No templates! (a debugging nightmare)
- No Boost/STL to deal with
- Less time spent building: only build the module you are working on.
- Ability to upgrade a base class *without* breaking derived classes in other dll's
Mono on Linux got *much* more interesting with the release of Mono Develop:
- Visual Studio clone
- Runs on Windows, Linux, OSX
- Works with C# and C++ (and other .net lang.)
- Reads VS solution files
- Reads makefiles
- http://monodevelop.com/
Memory management: not sure what issue you are talking about. C# doesn't leak, and most C++ programs do...so C# can reclaim memory that is lost forever in a C++ app.
Brutally difficult to interface existing libraries? Looks pretty simple to me:
http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
Posted by David | May 14, 2009 1:46 AM
Posted on May 14, 2009 01:46
You've almost sold me, *almost* :)
Clarification:
Memory management: When you're writing games you often have a requirement to explicitly manage memory (use these 10MB I allocated for you, or this particular heap).
Brutally difficult:
It's actually very easy for C structs and classes, as that link points out. It all goes cockeyed when are dealing with polymorphic C++ objects and the STL. I'm disappointed that Microsoft didn't simply provide adaptors for the most common STL objects such as std::string. The pedants will argue that since they are templated there is no way to anticipate every possible variation of every templated thingamajig. The practicals will argue that usually you use std::string as is... Why not support it?
Posted by nick | May 14, 2009 12:59 PM
Posted on May 14, 2009 12:59
I have developed very complicated applications in C# and I would never go back to C++ now. It is much faster to develop in and much easier to debug.
I don't care if the application can run 5% faster in C++, because 50 ms vs 47.5 ms is no big deal.
Posted by Patrick | November 25, 2009 10:59 AM
Posted on November 25, 2009 10:59
It is really nice written article from you guys. I would suggest it to everyone who wants to read and learn more. There is so many helpful information’s which I couldn’t find on other websites and other similar blogs. Thank You for that.
Posted by web design sydney | February 12, 2010 9:34 PM
Posted on February 12, 2010 21:34
If speed is your only deciding factor, then it obviously depends on what your program does. If it's a complex algorithm that you need performed as fast as possible, it's C++, no brainer. But if your program is simply a Twitter client, for example, then speed isn't really going to help you at all. The speed up in development is just worth it for you. Computers are just getting faster anyways and some unnoticable slow downs aren't going to destroy your product when you are counting in milliseconds.
Posted by Dominic K | February 13, 2010 9:58 AM
Posted on February 13, 2010 09:58
C# is based on C I don't understand why people think its slower than C++ perhaps it would be easier to acknowledge C# as managed C++ language?
Most your points are based on skepticism or lack of use, some article compare C# as equivalent contender to C++ and some commercial games have actually been built using C#.
On another factor C++ speed is not all that it is cracked up to be, too many operational run time problems in actual practice. Need proof? Just run any game written in pure C++, the only thing that has saved C++ in the long run thus far is the fact its old and that we now have multi-threaded processors.
Posted by Meh | January 30, 2012 6:03 AM
Posted on January 30, 2012 06:03