Hi, I am relatively new to C#.NET and I was wondering if the garbage collection is similar to Java.

I am developing a windows form application and I am dynamically making new buttons, will they be deleted by themselves or will I manually need to delete them?

For example:
this is declared as global
Button[] Letters = new Button[26];
if I have a function call initLetters() that does this:
Letters = new Button();

if I call initLetters() again will it delete the previous array? overwrite the array?

Thanks for your time.

Recommended Answers

All 2 Replies

I'm not completly sure what you mean with the button examples, but I think aslong as the component container is created in the scope with the buttons, when it goes out of scope, the buttons will be deleted. If your button array is global, then i think they will probably be deleted only after nulling them or creating a new button instance in the array location. Hope I helped, but as i said, I'm not completly sure i understand what your asking, so sorry if i have just jibbered on a bit about something completly unrelated.

Hi, I am relatively new to C#.NET and I was wondering if the garbage collection is similar to Java.

Yes.

I am developing a windows form application and I am dynamically making new buttons, will they be deleted by themselves or will I manually need to delete them?

For example:
this is declared as global

Button[] Letters = new Button[26];

That will make an array whose values are null.

if I have a function call initLetters() that does this:
Letters[i] = new Button();

That will set the i'th element of the array to point to the newly created Button object.

if I call initLetters() again will it delete the previous array? overwrite the array?

It will do what is written. The expression new Button() will construct a Button object and return a reference pointing to that object. In Letters[i] = new Button();, the value in Letters[i] will be set to that reference.

If a reference to a Button object was already stored in Letters[i], that reference will be overwritten. The Button object to which it previously refered will, if no other references to it exist, eventually be finalized and garbage collected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.