user2097371
user2097371

Reputation:

Using pointers with a class instance in c#

I recently switched over from C++ to C# and I am wondering how I would do the equivalent in c#. In C++ I could have done this:

Enemy *enemy; 

enemy = new Enemy("Goblin", 20, 20); 

In c#, I tried the pointer methond and using a delegate and both failed. The thing is I have multiple enemys in my Text RPG and I need to assign a specific enemy to my enemy pointer class so I then can preform the battle processes.

Upvotes: 2

Views: 2864

Answers (1)

ipavlic
ipavlic

Reputation: 4966

C# has references instead of C++ style pointers. So for your example, you would just do:

Enemy enemy; //enemy is a reference to an Enemy
enemy = new Enemy("Goblin", 20, 20); //the reference points to a Enemy instance in the heap

Another interesting difference is that almost everything is a reference, apart from some primitive value types (int, float, double, decimal, bool, structs, enumerations) which can be stored on the stack.

Upvotes: 4

Related Questions