Posted 17 January 2011 - 11:36 PM
Hey Soap,
I also struggled with this exact same issue when I first migrated into the C# Programming language from C++. There is no real short answer to your question but maybe I can help with a few small theories.
Firstly,
C# is a programming language designed to look feel and operate almost identically to the C++ syntax. C# keeps most if not all of the same syntax and basic theory of C++ but wraps it all into a simple and effective object hierarchy. These core objects are provided through standard libraries, such as the System objects and third party libraries such as Microsoft's .NET and XNA Libraries. To kinda bring this into perspective think of C++ Header files. Including conio.h gave you cout, in C# you Reference the System library and simply call "using System" which gives you access to the Console object.
Brings us to Library Referencing,
Next, an important difference is how you link to external libraries. In C++ You are used to including header files which contain decelerations and either linking a dynamic library or compiling the source file that defined the libraries functionality. C# takes library linking (referencing) into account in it's core design to ease this entire concept. There are no longer any header files, in C# you simply add a reference to the dynamic library that contains the methods you want and use the name space in your source. This works for linking thousands of standard and Microsoft libraries that will help you with all of your Microsoft Platform game writing needs. If and when you need to write a reusable library it is much easier then C++. You no longer need to worry about importing and exporting memory locations of the functions you are looking for. The C# Compiler will automatically write all that into your executable for you.
Dynamic Memory Allocation,
If you have ever done some serious coding you would also be familiar with allocating and releasing dynamic memory. Again this is all accounted for in the core concept of the C# programming language. The C# programming language has a built in garbage collecting system that will automatically release memory blocks that are no longer referenced by any other objects. The easiest way to explain this from one coder to another is with an example. Take C++ and say you where building a character object which requires a dynamic "Weapon" member. When your character equips a weapon you create a new object in memory and save a pointer to that location. When the player removes the weapon you want to delete that weapon object from memory, so you would delete the object and set the pointer to null. This is common practice and as such has been built directly into C#. C# No longer uses pointers, instead ALL objects are treated internally as references to dynamic memory. Therefore simply creating the object automatically creates it in dynamic memory, the variable that you used to create this object IS the pointer. When you set that object to null the dynamic memory is automatically free'd by the garbage collector. In C++ you need to worry about WHEN you free dynamic memory and not to leave any lingering pointers to a dead memory slot. C# again handles all of this for you. To pass a "pointer" to your variable you simply pass the variable itself, since ALL objects are dynamic in nature you are always passing a reference to the single instance in memory. You can then set any references to null when you are done with them, the garbage collector will only free memory that is no longer referenced by any other objects.
That's really the main things that you will need to come to terms with to start becoming more efficient with the C# Programming language. If you (or any other readers for that matter) have any questions please feel free to send me an email and I will see what I can do to help you out.
E-Mail: Dan@Digivance.com