Today is Saturday, 23rd November 2024
Sponsored:

C# Vs C++

Author: Naota

So you are interested in learning C-Sharp and already have some experience with C++?  Well you might have some questions on what the differences are between the two.

The two program languages look very similar when using an IDE (integrated development environment) like Microsoft’s Visual Studio .Net but under the hood is a different story.

C# Is Argued To Be Slower Than C++ But Has Been Optimized
It is good to note that C# is often found to be slightly slower than its c++ counterpart. Some users claim as much as a 10% reduction in application speed. Overtime, this speed reduction has been slightly mitigated thanks to additional features and available add-ons.

The following is an excerpt taken from university professor, Michael Suess, in his Article Titled, “C++ vs. C# – a Checklist from a C++ Programmers Point of View” . This article focuses on the differences between C# VS C++.

Pro C#:

  • garbage collection
  • array bounds checking
  • huge .NET-Framework library
  • types have a defined size (e.g. a long is 64Bit)
  • strings are encoded in UTF/16
  • autoboxing – every type can be treated as if it inherits from object
  • supports constructor-chaining (one constructor can call another constructor from the same class)
  • when a virtual method is called in a constructor, the method in the most derived class is used
  • static constructors (run before the first instance of the class is created)
  • exceptions have access to a stack trace
  • advanced runtime type information and reflection
  • supports variadic functions nicely
  • built-in support for threads
  • no need for header files and #includes
  • no fall-through on switch-statements
  • arithmetic operations can be checked for overflow if required
  • objects must have a definite value before being used
  • attributes can be attached to classes and retrieved at runtime
  • no forward declarations required, classes can be arranged at will
  • access to class members / functions is done only by the dot (no more -> or ::)
  • conditional functions (e.g. for debugging)
  • structs and classes are actually different (structs are value types, have no default constructor in general cannot be derived from)
  • supports properties
  • readonly members are const, but can be changed in the constructor
  • finally block for exceptions
  • arrays are objects
  • support for anonymous functions
  • supports the base keyword for calling the overridden base class

Pro C++

  • better performance
  • portability
  • multiple inheritance
  • deterministic destruction (allows RAII)
  • any type can be thrown as exception (only classes derived from System.Exception in C#)
  • ability to enforce const-correctness
  • implicit interfaces on generics (in C#, generics must be constrained with an interface)
  • offers pointers (C# only offers pointers in unsafe mode)
  • support for macros
  • support for global variables, functions, constants
  • allows default arguments on function parameters
  • STL
  • supports bitfields

Where C# is just different from C++

  • value types and reference types exist (struct is value-type, class is reference-type)
  • value types live on the stack, reference types on the heap
  • references can point to null (must not be valid)
  • code is packaged in assemblies in C#
  • no automatic conversion from int to bool in C#
  • main-function is called Main in C#
  • no semicolon after a class declaration in C#
  • everything derives from object or can be treated as if

Professor Suess also included the resources he used while conducting his research. I have found many of these links to be useful and have included them on this page as well, and would like to give credit where credit is due.

Resources

This list of course is not exhaustive. There are many more differences. C# does have limits, but as long as you aren’t doing anything too outrageous, than you should be fine. C-Sharp is a very clean, useful and enjoyable programming language to learn. That concludes this article.

Make sure to check out the next article and learn how to start coding!

– ナオ太 (Naota)


1 Comment

  1. Comments  mobile phones   |  Wednesday, 19 December 2012 at 7:39 AM

    Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?

    I’ve been trying for a while but I never seem to get there! Thanks

Leave a Reply





Sponsored

Affiliate Articles:

Amazon Deals

Top