c# coding

  • Thread starter Bob Powell [MVP]
  • Start date
B

Bob Powell [MVP]

//allocate
thing X=new thing();

//deallocate
x.Dispose(); //(optional if object implements dispose pattern)
x=null;

In managed code, memory is deallocated by the garbage collector when it's no
longer needed.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
C

clintonG

We don't need to alloc anymore which is why develolping ASP.NET is called
Managed Code. As for examples you should spend some time learning what
ASP.NET actually is and how the compiler works.

//try this to get started using the overview keyword
//Microsoft uses this keyword for --everything-- we want
//to learn about

//search
asp.net overview site:msdn2.microsoft.com

//search
managed code overview site:msdn2.microsoft.com
 
R

Robert Fuchs

why are you talking about ASP.NET?
Nobody asked for that.
The OP asked about C#...

regards, Robert
 
R

Richard Blewett

Bob Powell said:
//allocate
thing X=new thing();

//deallocate
x.Dispose(); //(optional if object implements dispose pattern)
x=null;

In managed code, memory is deallocated by the garbage collector when it's
no longer needed.

--

If the OP really wants to talk about *memory* then the call to Dispose does
nothing about memory but does allow the object to clear up non-memory
resources.

The setting of x to null is unnecessary and doesn;t do anything at all as x
is a local variable. If x was static then setting to null would be vital in
allowing the object to be cleaned up. If x were a member variable then
setting it to null *might* be worth it

--
Regards

Richard Blewett
DevelopMentor
http://www.dotnetconsult.co.uk/weblog2
 
J

Jon

"but does allow the object to clear up non-memory resources"
Which may themselves use memory.


Bob Powell said:
//allocate
thing X=new thing();

//deallocate
x.Dispose(); //(optional if object implements dispose pattern)
x=null;

In managed code, memory is deallocated by the garbage collector when it's
no longer needed.

--

If the OP really wants to talk about *memory* then the call to Dispose does
nothing about memory but does allow the object to clear up non-memory
resources.

The setting of x to null is unnecessary and doesn;t do anything at all as x
is a local variable. If x was static then setting to null would be vital in
allowing the object to be cleaned up. If x were a member variable then
setting it to null *might* be worth it

--
Regards

Richard Blewett
DevelopMentor
http://www.dotnetconsult.co.uk/weblog2
 
B

Bob Powell [MVP]

thing X=new thing();
x
is a local variable.


Do you see a class declaration in there??

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
J

Jon Skeet [C# MVP]

Bob Powell said:
x
is a local variable.


Do you see a class declaration in there??

No, but then I didn't see a method declaration either.

If X is really meant to be a local variable, then setting it to null is
almost certainly a bad idea - and if Thing implements IDisposable,
then a using statement would almost certainly be more appropriate than
manually calling Dispose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top