Reply to post: Re: Null pointers

Mads Torgersen and Dustin Campbell on the future of C#

AndrueC Silver badge
Thumb Up

Re: Null pointers

Actually, yes, similar to references. It's been a few years so they'd slipped my mind. But C# pointers are not quite the same because it's a garbage collecting language. They are more akin to smart pointers. We do instantiate a class via new:

var fred=new Person();

but when we pass an object to a method:

kill(fred);

We don't think of it as passing a pointer to fred, we just think of it as passing fred.

And if Kill() wants to take its own copy:

void kill(Person aPerson)

{

_myListOfKilledPeople.Add(aPerson);

...do the actual killing.

}

It's not a problem. Dear old fred will hang around for at least as long as he's needed. The only downside is that we won't know exactly when he finally pops his clogs. On the occasions where that matters using() is the closest we have to RAII, though most people seem to use try/finally instead :(

Pointers generally only crop up in unsafe code eg; talking to the Windows API. But even there p/invoke does a pretty good job of hiding pointers as well:

// C++ (NativeLib.dll)

void print_line(const char* str);

// C#

[DllImport("NativeLib.dll")]

private static extern void print_line(string str);

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon