Hello friends
this is softy.
I am a C# developer, a number of times I have come across the question that is C# more advantage than C++ and do we have use of pointers in C# ?
Please let me know if you have the answers.
Thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AnttiPosted Feb 8, 2006, 6:36 AM
Classes are reference types, and in .NET a reference stands for a "managed pointer". Garbage collector has information about object references in stack memory, and uses them to keep alive or delete objects located in heap memory.
Structs however (and all other value types), are not reference types, and they got no managed pointer in similar meaning. Value types are located directly in stack memory, and CRL deletes them from there.
>>You can use the "ref" keywork to use references. That's not exactly the same thing as pointers, but as I know it's the closest form that C# allows.
Thats not quite true. You can indeed override the garbage colletion mechanism, and use POINTERS directly in your C# program. The pointers have similar syntax than in C++, *, & and so on. You must wrap pointer handling code inside the unsafe block. But pointers are seldom if never needed in C# programs, since using pointers does wreck the whole idea of safe and automatic garbage collection, don't you think?
I myself would not recommend the direct use of pointers in C#. Only situations when you ever would need them, would be the use of C-generic dll's and hard-core code optimization. Optimization with pointers still requires you yourself to handle the memory management! Writing a good code without pointers and with good algorithms is better than risky unmanaged memory. Managed code indeed stands for that someone (CLR) manages the memory for you.
James SPosted Feb 7, 2006, 7:25 PM
SimonPosted Feb 7, 2006, 7:24 PM
Simon