BannerLeft BannerLeft

 

C#

This area of the site is about the Microsoft language C# (C-Sharp). It is intended for those, typically game programming enthusiasts, who already know C++.

This section of the site is currently being added to and  will develop more fully over the course of the next few months.

Why use C#

Well firstly it is probably best to ask why not use C#? What are the problems with it? Well firstly it is what Microsoft call a managed language in that it does not compile down to native code. This means that on each supported platform there must be a kind of virtual machine that translates the C# code as required at runtime. It is very like Java in this way. Of course the problem with this is speed. It will never be able to run as fast as a native language like C++.

For games development C# is not really fast enough for the latest demanding games. However for less demanding games requiring rapid development it could be very useful. Currently it is not widely used in the games industry for game development however it is being used quite extensively for tool development. Note that C# can interface with native language binaries via its interop capabilities.

The great advantage to C# is that it promotes rapid development. It is very easy and quick to knock up a game tool or even a game demo using the XNA additions. It is also a real joy to develop with (I spent some time last summer learning it and found it really easy to work with and with a lot of very nice features that I now miss when going back to C++ programming). Being a managed language and a new language it integrates into Visual Studio in ways that native languages cannot hope to. Intellisense finally works properly! Refactoring is even built in and there are many other useful tools to aid development.

What are the main differences between C# and C++

As this site is mainly dedicated to C++ programming I am assuming you already know how to program in C++ (or some other object oriented language) but are interested in trying out some C#. Therefore I will concentrate on C# from a C++ programmers perspective.

An awful lot of C# is very familiar to a C++ programmer but some of the first things you will notice in C# as being different to C++ are

  • No header files - the class definition and implementation are now normally in the same cs file (you can split it between files using the partial keyword if required).
  • No pointer notation - while there is no obvious pointer notation in fact all class instances in C# are referred to via a pointer (or reference) therefore you must use new to create all objects. However the basic language types and structures go on the stack and are declared and used as in C++. E.g. int a, See references below for more details.
  • Memory management - C# handles memory allocation and deallocation automatically (via garbage collection). There is no need to worry about deleting memory any more! This removes a large headache from C++ programming but does take a bit of getting used to. I still feel a bit uncomfortable not quite knowing when memory will be deleted! However be assured that it will only be deleted when you no longer need it.
  • You can initialise class variables when you declare them
  • foreach - an additional loop construct similar to iterators in C++
  • To access enums you must use the enum type name followed by a . e.g. EGameMode.eStartScreen
  • You cannot use default parameters in function calls
  • You can only have static class variables in static classes

Other less obvious differences are

  • No multiple inheritance. While C# does not allow multiple inheritance it does allow multiple inheritance of interfaces. Interfaces are just like the design pattern you may have used in C++ where just the required functions are defined (and they are all pure virtual) however in C# this is made explicit via the use of the 'interface' keyword
  • Every type (even your own) is ultimately derived from System.object
  • No templates. While there are no C++ style templates C# does support the feature via the use of what is called generics
  • Accessing statics in C# is done using . not ::
  • Parameter types. C# has a few parameter types not found in C++. Out is used to state a parameter to be returned. It must be assigned to. Ref is like out but must be initialised before passing to the method which may optionally reassign it. Params allows a variable number of parameters to be passed, similar to the ... from C. See references below for more details.
  • Virtual can be used as in C++ but a pure virtual function is declared using the abstract keyword. To override a function use the 'override' keyword in the child definition. Note that pure virtuals can only be used in abstract base classes.
  • Structures always go on the stack. While classes cannot be instantiated on the stack (they must use new and go on the heap) basic language types and structures go on the stack. What is really interesting is that even if you use new with a structure it will go on the stack.
  • Arrays are reference types and provide a lot of extra functionality

References in C#

It can be confusing to start with for a C++ programmer using C# to know when something is a reference or not. Basically all language types like int, float, bool etc. are not references and are used just like they are in C++. They also go on the stack like in C++.  If you pass them to a function a copy is made (like in C++). If you do want to change them in the function you would need to use the ref keyword in the function definition and when calling the function. Class instances are references and must be created using new and are put on the stack and may be null. If you pass them to a function the function can alter the instance data but not the reference itself - no copy of the instance data is made. So thinking in C++ terms all class instance variables are pointers while all language types are not.

Slightly confusingly structures are treated just like language types and go on the stack (even when created with new). This is quite different to C++ where a structure is basically a class with all public data.

Other cool things in C#

  • You can get the type of a variable using typeof. You can also use is e.g. if (var is CEntityPlayer) and as to see if the types are compatible
  • @ can be used prior to a string to mean treat it as verbatim. So no more double slashes in filenames!
  • Can use the base keyword anywhere in the child to access the base class
  • Properties - class variables can be public, private or protected like in C++ but C# provides a mechanism for creating a property. This is basically like making get and set functions in C++ but takes away some of the pain.
  • yield - this keyword acts as an iterator by remembering the current location in the collection. This is great as you can call a function repeatedly and it remembers where it got to (in C+ this requires either the use of a member variable or passing the current location as a pointer)
  • C# handles callbacks using delegates which means you can provide class functions as callbacks (even if not static!). Note that events are also available.
  • Serializable - this really sold me on C#! By placing [Serializable] above a class definition it will automatically be saved correctly! I used this in a project where all the data hung off one class. By making them all Serializable I could save everything with one simple call to save the main class. Automatically every variable and every class instance below that class get saved! You can also deserialize in the same easy way.
  • Auto implemented properties. How many times in C++ have you had to write get and set functions for class member variables? C# provides auto-implemented-properties that saves a lot of typing: e.g.
  • class Player
    {
        public int Health { get; set; }
    }

Libraries

One of the greatest features of C# is the huge number of libraries it comes with. These support all sorts of operations and are very well designed. An example is every type has a "ToString" member function allowing it to be converted to a string. In C++ this must be done by creating specific functionality (see string handling). In C# you can simply write "The value of a is "+a.ToString();

Useful library types include

  • IO library for file and memory input and output
  • String and string collection
  • Array, array list,  list, stack, queue etc.
  • BinarySearch

To use libraries you state you want them via the 'using' keyword like in C++ where you include names from a namespace.



© 2004-2014 Keith Ditchburn