C++ to C#

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Since I'm doing all my work in C# and I don't know C++ much, if at all anymore, I'm going to keep an on-going log here of things I run into that are different, and how I solved them in C#. Hopefully this will help anyone else that is reading this book and want to do that game in C#. If my interpretation of something is wrong, please let me know. :)

      typedef

      This basically creates an alias for a type.

      Source Code

      1. typedef unsigned in ActorId;


      In C# there are 2 ways to handle this.

      1. using alias. This only works within the file the code resides.

      Source Code

      1. using ActorId = System.UInt32;


      2. Create a new type.

      Source Code

      1. internal struct ActorId : IEquatable<ActorId>
      2. {
      3. private readonly int id;
      4. private ActorId( int id )
      5. {
      6. this.id = id;
      7. }
      8. public static implicit operator int( ActorId actorId )
      9. {
      10. return actorId.id;
      11. }
      12. public static implicit operator ActorId( int id )
      13. {
      14. return new ActorId( id );
      15. }
      16. public bool Equals( ActorId other )
      17. {
      18. return id == other.id;
      19. }
      20. public override bool Equals( object obj )
      21. {
      22. if( ReferenceEquals( null, obj ) )
      23. {
      24. return false;
      25. }
      26. return obj is ActorId && Equals( (ActorId)obj );
      27. }
      28. public override int GetHashCode()
      29. {
      30. return id;
      31. }
      32. public static bool operator ==( ActorId left, ActorId right )
      33. {
      34. return left.Equals( right );
      35. }
      36. public static bool operator !=( ActorId left, ActorId right )
      37. {
      38. return !left.Equals( right );
      39. }
      40. public override string ToString()
      41. {
      42. return id.ToString();
      43. }
      44. }
      Display All



      shared_ptr

      This is a normal object in C#. The .NET garbage collector automatically cleans up objects.

      weak_ptr

      WeakReference<T> msdn.microsoft.com/en-us/library/gg712738(v=vs.110).aspx

      MonoGame uses .NET 4.0 which only has the non-generic version of WeakReference. I just created my own generic version that wraps the non-generic one.

      interface

      C++ doesn't have interfaces. If you create a class with all abstract methods, it would be considered an interface. This book uses all virtual methods, but some methods are implemented. Any methods that overriding is required are set to 0. This part confused me at first.

      Source Code

      1. virtual HRESULT VOnRestore() = 0;


      In C# you can use a real interface. If you need to have a method with some implementation, use an abstract class instead.

      destructors

      In C#, destructors are called finalizers, and should not be used 99.9% of the time. Take a look at what Eric Lippert has to say about them; as he wrote the C# compiler. stackoverflow.com/a/4899622/68499

      Instead, implement IDisposable. msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx

      collections

      These are the equivalent STL collections in C#.

      Source Code

      1. [] -> Array
      2. std::vector<T> -> List<T>
      3. hash_map<Key, Data> -> Dictionary<TKey, TValue>
      4. hash_set<Key> -> HashSet<T>
      5. std::map<Key, Data> -> SortedDictionary<TKey, TValue>
      6. std::vector<T> -> SortedList<TKey, TValue>
      7. std::set<Key> -> SortedSet<T>
      8. std::queue<T> -> Queue<T>
      9. std::stack<T> -> Stack<T>
      10. std::list<T> -> LinkedList<T>


      A concurrent_queue is also used. C# has a few concurrent collections built in.

      Source Code

      1. BlockingCollection<T>
      2. ConcurrentBag<T>
      3. ConcurrentDictionary<TKey, TValue>
      4. ConcurrentQueue<T>
      5. ConcurrentStack<T>


      delegates

      The book creates their own delegates using FastDelegates from CodeProject.

      Source Code

      1. typedef fastdelegate::FastDelegate1<IEventDataPtr> EventListenerDelegate;


      C# has delegates built into the language. There are even 2 really useful built in delegates, Action, and Func<TResult>. Action optionally takes in parameters and doesn't return anything. Func optionally takes in parameters and returns a value.

      The delegate code could be used like this.

      Source Code

      1. Action<IEventData>


      Generics start to get ugly when you nest a bunch of them. It's just as easy and much cleaner to use if you create your own delegate.

      Source Code

      1. public delegate void EventListenerDelegate( IEventData );

      The post was edited 8 times, last by narshe ().