« In Search of the Perfect Keyboard | Home | IMDb Credit »

Individual Entry With Comments


May 12, 2009

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:

  • Different entry point (main/wmain)
  • Prefixing text with “L” or “_T”
  • New string manipulation functions

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

  • For type T with null support, use type T?
  • For example, int with null support is int?
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:

  • No memory leaks
  • Can’t read uninitialized variables
  • Can’t index past array boundaries
Types do not share a common root All types derived from base “object”

 

Comments (8)

Bubba:

You lost me at Hello World!

nick:

I know you posted this to bait me, but I just can't help myself.


C++ C#
--------------------------
Fast Slow

Runs on handheld Yes, I know
devices about mono, &
have no comment

Explicit memory I hope you have
management enough memory

Easy to interface Brutally difficult
to existing C/C++ to interface to
libraries existing libraries

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

nick:

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?

Patrick:

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.

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.

Dominic K:

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.

Meh:

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.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


About

This page contains a single entry from the blog posted on May 12, 2009 8:35 PM.

The previous post in this blog was In Search of the Perfect Keyboard.

The next post in this blog is IMDb Credit.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34