Friday, June 16, 2006

Common issues and traps of C# for a C++ programmer

The most noticeable thing about C# is the difference between reference types and value types.


Value types are types allocated and passed on the stack,
things like: int,char,double,enum etc

Reference types are types which are managed i.e. class, delegate and are allocated on the GC managed heap (subject to garbage collection)

Boxing

Boxing is taking a value type (on the stack) and assiging it to a reference type (onto the managed heap)

example:

object x = new object;
x = 10;

Here a 10 is actually copied across from the stack onto the managed heap memory

Unboxing

Unboxing taking a value from a managed object (on the managed heap ) and assigning to a value on the stack

void
function (object obj)

if (obj is int)
{
int x = (int) obj;
....
}

Being a C++ programmer you know when an object is on the stack or on the heap However in C# you seem to be given choice only at the type declaration level and you have to realise when boxing and unboxing is taking place because it is ofcourse slower since memory has to be copied rather then simple memory assignment.
The Java community argue that it should be up to the runtime to decide when objects are passed by value(on the stack) or should be allocated on the heap. But what I don't understand is how can the runtime know when this object is to be passed by value? Perhaps a Java VM Guru can explain how the Java VM decides surely there are performance implications when the object has to be copied from the stack to the heap?

Anyway The key difference between C++ and C# is a struct is a value type (on the stack) a class is a managed reference type (on the heap)

Choosing if a type should be a struct (stack) or a class (heap) has to be done very carefully

I have been bitten by this! take a look at the following code:

private List < SomeType > mListOfSomeType;

void MarkDataAsDirty(bool dirty)
{
foreach(SomeType member in mListOfSomeType)
{ member .Dirty = dirty; }
}

if SomeType is a struct, The above code will not actually set the dirty flag on the members of the list
In order to acheive this one has to change the code to.

void MarkDataAsDirty(bool dirty)
{
for(int i = 0; i < mListOfSomeType.Count; i++)

{ mListOfSomeType[i].Dirty = dirty; }
}

Here are the ten common traps C++ programmers fall into when writing c# code (the struct and class issue is one of them)

http://www.ondotnet.com/pub/a/oreilly/dotnet/news/programmingCsharp_0801.html

Background

I have started this blog as a diary of my findings of C# as a C++ programmer.

The blog assumes you are familiar with C++ and c# and goes into quite some depth.

I am well aware each tool has its own purpose C++ is a very powerful language but one has to appreciate that is all it is, a language. This is not really a comparison of the languages but a relflection on common issues that a developer encounters working with these languages.


This blog will probably end up with:


  • References to material that is useful to understand C# programming for a C++ programmer
  • Features of .NET that I find extreemly useful that can never exist in a traditional C++ development environment
  • A wish list of what I would like in C# as a developer using an OO language that I have in C++

Before reading the rest of the blog I suggest you read the following article which gives an overview of C# for a C++ programmer

http://msdn.microsoft.com/msdnmag/issues/01/07/ctocsharp/