views:

72

answers:

2

I'm a C++ user and i started a few ago with C#, to be able to use XNA, because it makes really easy working with 2d textures and 3d models, so that i can forget a bit about that and just center in the game code.

The problem comes that I'm used to the pointers, so that my way of thinking the code usually makes use of some, and I couldn't find the way to use them without switching the managed code to the unsafe one, which i wish to keep this way.

Also considered using the unsafe one, but for example it disables the garbage collector, and I couldnt find the way then, to use an equivalent statement to C++'s delete, any idea would be apreciated, thanks in advance! :)

+4  A: 

All objects in C# are pointers, except instances of structs and built-in data types like int, double, bool, etc.

So, when you pass an object to a function it behaves like a pointer in C/C++, so it is sent by reference. But built-in data types, are sent by value.

Hamid Nazari
Any `struct` type (value type) is also passed by value, in addition to the numeric value types.
Matti Virkkunen
@Matti Thanks, I'll fix that.
Hamid Nazari
You can use the ref keyword to pass a value type by reference.
Merlyn Morgan-Graham
A: 

So thank you, I just tried this piece of code and it really works! :D (I asked the question before programming, not to spoil the code if it wouldn't work, as I wrongly expected :P)

playerList.playerAt(0).Score = 0;
Player self = playerList.playerAt(0);
playerList.playerAt(0).Score += 50;
Console.WriteLine("Your score is {0}",self.Score);
Dane411
Answers are meant to be ... answers ;). Comment instead of answer.
Dykam