C# .NET - why pointer is not much used in c# - Asked By Ajain A K P on 19-Dec-12 08:12 AM

I heard pointer is not much used in c#,is it correct??? if so.. what is the other alternative to be used instead
of pointer. Why pointer is not much utilized. can somebody help to get some detail on this

Thanks in advance.

Robbe Morris replied to Ajain A K P on 19-Dec-12 08:12 AM
The .NET framework manages this sort of thing behind the scenes (well, sort of).  It makes it much simpler to code in versus something like C++ where you need to manage this on your own.  .NET still has the concept of "object references" which would equate (sort of) to pointers in C++.  The difference is that you do not need to manage them.  The garbage collector will clean things up as references go out of scope or are no longer in use.
Ajain A K P replied to Robbe Morris on 19-Dec-12 09:22 AM
Dear Robbe,

Thanks a lot for very good reply. Well I have a C project having lot of pointers in the code, basically the project fuctionality is mostly file handling with good memory utilization. If I want to convert this project to c#,
can I use "object reference" instead of all these pointers  OR any other method should I apply. Kindly
adive me

expecting your reply...........  
Robbe Morris replied to Ajain A K P on 19-Dec-12 09:34 AM
There is no explicit conversion.  .NET just magically handles it.  In the code below, it demonstrates the simplicity of passing around a class, altering it, and then outputing its current state.  All without wiring up pointers.

 
 public void Main()
 {
    var myClassVariable = new MyClass();

    DoSomethingToMyClass(myClassVariable);

    System.Diagnostics.Debug.WriteLine(myClassVariable.FirstName);

   DoSomethingElseToMyClass(myClassVariable);

   System.Diagnostics.Debug.WriteLine(myClassVariable.FirstName);

 }

 public void DoSomethingToMyClass(MyClass myClassVariable)
 {
   myClassVariable.FirstName = "stuff";
 }

 public void DoSomethingElseToMyClass(MyClass myClassVariable)
 {
   myClassVariable.FirstName = "some different stuff";
 }
 
 
Ajain A K P replied to Robbe Morris on 20-Dec-12 11:40 PM
Dear Robbe,

Thank you very much for the answer, It was very informative. So Sorry for the delay....from my side